WPF在DataGrid中绑定到DataContext

时间:2021-01-17 19:38:37

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

我有一些问题将datagrid组合框ItemsSource映射到主datacontext。这是一些显示问题的演示代码。

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 ItemsSource to 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/

我也试过(使用相同的relativesource)来做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必须是List,所以我不能枚举。

Anyone know how to fix this binding?

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

1 个解决方案

#1


10  

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>

#1


10  

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>