6
votes

I am trying to databind DataGridComboBoxColumn

<DataGridComboBoxColumn Header="Number of Copies" SelectedItemBinding="{Binding NumberCopies}">
    <DataGridComboBoxColumn.ElementStyle>
       <Style TargetType="ComboBox">
          <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
          <Setter Property="IsReadOnly" Value="True"/>
       </Style>
    </DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>

What I am doing wrong here , because I am getting an empty combobox in the run time .


I got following

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=LifeAreaList; DataItem=null; target element is 'DataGridComboBoxColumn' (HashCode=49475561); target property is 'ItemsSource' (type 'IEnumerable')

4
And what error(s) are reported in the Output window when this runs? - Henk Holterman

4 Answers

9
votes

DataGridColumn doesn't derive from FrameworkElement or FrameworkContentElement so it isn't in the visual tree and doens't have a DataContext and that's why your Binding is failing.

If the List<int> that you're binding to is the same for every item then maybe you should find another way to bind to it, maybe you could make it static and use StaticResource in the Binding.

Anyway, to bind ItemsSource to a List<int> property in your source class you can use ElementStyle and ElementEditingStyle (as pointed out by others). The following should work

<DataGridComboBoxColumn Header="Number of Copies"
                        SelectedItemBinding="{Binding ListAreaItem}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding LifeAreaList}"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
2
votes

You are not supposed to set the ItemsSource in the style, the column itself has such a property which may override anything you may try to set in the style. Further, you try to set it in the wrong style (that style is for the display mode), you could try setting it in the EditingElementStyle instead, but i would not recommend that either.

1
votes

Why are you setting Items source in style?

Can you try this code:

 <my:DataGridTemplateColumn Header="Number of Copies" >
                    <my:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=LifeAreaList}"  >
                               <ComboBox.ItemTemplate>
                                <DataTemplate>
                                    <Label Content="{Binding .}"></Label>
                                </DataTemplate>
                               </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>

Define Data template for DataGridTemplateColumn if LifeAreaList is complex class collection and you want to display it in customized way.

0
votes

I would try a regular DataGridColumn with PresentationTraceSources.TraceLevel="High" and see if you are are having a binding problem.