Binding WPF in a DataGrid with a DataContext

I am having some problems with matching datagrid ItemsSource with datacontext body text. This is a demo code showing the problem.

My item 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

 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" } }; } } 

Then create and assign it to the data context in MainWindows ()

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

on the xaml side, I create a simple datagrid to try to show it like this.

 <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> 

Does not display Combobox ItemsSource in DataContext.PriorityTypes

I also tried (with the same source of relatives) to do DataContext.PriorityTypes with no luck. I came across a few blog posts with no luck, but this shows the method I took. http://sekagra.com/wp/2013/04/dynamic-itemssource-for-combobox-in-a-datagrid/

This is a more simplified example of my problem, but the key part is PriorityType should be List, so I can not list.

Does anyone know how to fix this binding?

+6
source share
1 answer

I believe that the problem you are facing is when the DataGridComboBoxColumn does the data binding - based on the trace messages you cannot reach the parent at all. Replace this column with the one that uses the DataTemplate instead:

  <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 to 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> 
+9
source

Source: https://habr.com/ru/post/957451/


All Articles