C# DataGrid 中的 WPF 绑定到 DataContext

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19798183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:54:50  来源:igfitidea点击:

WPF Binding in DataGrid to DataContext

c#wpfxamldatagridcombobox

提问by jrich523

I'm having some issues mapping a datagrid combobox ItemsSourceto the main datacontext. This is some demo code to show the issue.

我在将数据网格组合框映射ItemsSource到主数据上下文时遇到了一些问题。这是一些演示代码来显示问题。

My items class

我的物品类

public class MyData
{
    public string Name { get; set; }
    public string Priority { get; set; }
}

I created a class to connect to the data context, and it looks like this

我创建了一个类来连接到数据上下文,它看起来像这样

public class myMV
{
    public ObservableCollection<MyData> MyItems { get; set; }

    public List<string> PriorityTypes
    {
        get { return new List<string> { "High", "Normal", "Low" }; }
    }

    public myMV()
    {
        this.MyItems = new ObservableCollection<MyData>
                       {
                           new MyData { Name = "item1", Priority = "Low" },
                           new MyData { Name = "item2", Priority = "Normal" },
                           new MyData { Name = "item2", Priority = "High" }
                       };
    }
}

I then create and assign this to the data context in the MainWindows()

然后我创建并将其分配给 MainWindows() 中的数据上下文

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new myMV();
}

on the xaml side i create a simple datagrid to try and show this like so.

在 xaml 方面,我创建了一个简单的数据网格来尝试像这样显示它。

<DataGrid Grid.Row="1" ItemsSource="{Binding MyItems}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Title" Binding="{Binding Name}" Width="*"></DataGridTextColumn>
        <DataGridComboBoxColumn Header="Priority" SelectedItemBinding="{Binding Priority}" ItemsSource="{Binding PriorityTypes, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
    </DataGrid.Columns>
</DataGrid>

It's not mapping the Combobox ItemsSourceto the DataContext.PriorityTypes

它没有将 Combobox 映射ItemsSource到 DataContext.PriorityTypes

I also tried (with the same relativesource) to do DataContext.PriorityTypes with no luck. I've come across a few blog posts with no luck but this one shows the method i took. http://sekagra.com/wp/2013/04/dynamic-itemssource-for-combobox-in-a-datagrid/

我也尝试过(使用相同的相对源)做 DataContext.PriorityTypes 没有运气。我遇到了一些没有运气的博客文章,但这一篇显示了我采用的方法。 http://sekagra.com/wp/2013/04/dynamic-itemssource-for-combobox-in-a-datagrid/

This is an over simplified example of my problem, but the key part is the PriorityType must be a List, so i cant do enum.

这是我的问题的一个过于简化的例子,但关键部分是 PriorityType 必须是一个列表,所以我不能做枚举。

Anyone know how to fix this binding?

任何人都知道如何修复此绑定?

采纳答案by Jaykul

I believe the problem you're having has to do with when the DataGridComboBoxColumn does it's databinding -- based on the trace messages, you're not able to get to the parent at all. Replace that column with one that uses a DataTemplate instead:

我相信您遇到的问题与 DataGridComboBoxColumn 进行数据绑定时有关——根据跟踪消息,您根本无法访问父级。将该列替换为使用 DataTemplate 的列:

     <DataGridTemplateColumn Header="Priority"  Width="100">
        <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
              <ComboBox SelectedItem="{Binding Priority}" ItemsSource="{Binding DataContext.PriorityTypes, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
           </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>

You can even get fancy and use a separate template for display...

您甚至可以花哨并使用单独的模板进行显示...

     <DataGridTemplateColumn Header="Priority"  Width="100">
        <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
              <Label Content="{Binding Priority}" />
           </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
        <DataGridTemplateColumn.CellEditingTemplate>
           <DataTemplate>
              <ComboBox SelectedItem="{Binding Priority}" ItemsSource="{Binding DataContext.PriorityTypes, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}}" />
           </DataTemplate>
        </DataGridTemplateColumn.CellEditingTemplate>
     </DataGridTemplateColumn>