6
votes

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?

1
Did you try just ItemsSource="{Binding PriorityTypes} ?James Sampica
yeah, because the datagrid has its itemssource set to a property of the datacontext it (the combobox itemsource) doesnt natively know to look up one level higher.jrich523

1 Answers

15
votes

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:

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