I'm having some issues mapping a datagrid combobox ItemsSource
to the main datacontext.
This is some demo code to show the issue.
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()
public MainWindow()
{
InitializeComponent();
this.DataContext = new myMV();
}
on the xaml side i create a simple datagrid to try and show this like so.
<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
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/
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.
Anyone know how to fix this binding?
ItemsSource="{Binding PriorityTypes}
? – James Sampica