6

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

H H
  • 263,252
  • 30
  • 330
  • 514
Night Walker
  • 20,638
  • 52
  • 151
  • 228

4 Answers4

9

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>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266
  • ,thanks for your answer it looks interesting. If I make my List static , how I can bind to it ? Could you please give me an example for this one. – Night Walker Aug 13 '11 at 15:36
  • @Night Walker: I'm not at my computer now but see the following link: http://stackoverflow.com/questions/936304/problem-binding-to-static-property – Fredrik Hedblad Aug 13 '11 at 15:49
  • Now as I think deeper static will not help me because I need to get the selected item in the combobox ,And I will have a problem If I start using statics for those also. – Night Walker Aug 13 '11 at 15:56
  • @Night Walker: That will not be a problem. The ComboBoxes will share the ItemsSource but they will hace individual SelectedItems so that part already works for you. Your SelectedItemBinding can stay as it is – Fredrik Hedblad Aug 13 '11 at 16:02
  • Thanks your example is really helpful and solved my issue . Could you please add also an example with static binding. I have tried and it not so worked in my case but probably I did something wrong (too new to xaml) – Night Walker Aug 13 '11 at 20:02
2

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.

H.B.
  • 166,899
  • 29
  • 327
  • 400
1

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.

RockWorld
  • 1,278
  • 2
  • 11
  • 24
0

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

paparazzo
  • 44,497
  • 23
  • 105
  • 176