Reputation: 21280
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')
Upvotes: 7
Views: 4524
Reputation: 109
If you would to use something similar to DataGridCobmoboxColumn
may it be better to use the DataGridTemplateColumn
you can use it and have the same functionality without need to use for ItemsSource
StaticResource
.
I set the root element in RelativeSource
as UserControl
, if your DataGrid
is located inside a Window
or Page
, you will need to use them accordingly.
<DataGridTemplateColumn Header="Number of Copies">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding NumberCopies}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedItem="{Binding NumberCopies, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding DataContext.LifeAreaList, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Upvotes: 0
Reputation: 84684
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>
Upvotes: 9
Reputation: 185553
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.
Upvotes: 2
Reputation: 45106
I would try a regular DataGridColumn with PresentationTraceSources.TraceLevel="High" and see if you are are having a binding problem.
Upvotes: 0
Reputation: 1288
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.
Upvotes: 1