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?