Reputation: 1713
I am succesfully binding a collection to a DataGrid and also succesfully binding a property to a DataGridComboBoxColumn. (There is a WPF tool called snoop that allows me to investigate if the data has been bound).
But for some reason the initial data is not being shown. Only after I change the selection manually. The value is visibly available.
Any tips or help is appreciated !
Thank you,
Here is my XAML:
<DataGridComboBoxColumn Width="*"
DisplayMemberPath="RedOms"
Header="MyHeader"
ItemsSource="{Binding Source={StaticResource MyModel},
Path=SRCollection,
Mode=OneWay}"
SelectedValueBinding="{Binding AZSR,
Mode=TwoWay}"
SelectedValuePath="ID">
<DataGridComboBoxColumn.CellStyle>
<Style BasedOn="{StaticResource EDGridCell}" TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding AZBev, Mode=OneWay}" Value="False">
<Setter Property="Background" Value="{StaticResource KlrColor}" />
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridComboBoxColumn.CellStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="Background" Value="{StaticResource KlrColor}" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
Here is Static Resource EDGridCell
<Style x:Key="EDGridCell" TargetType="{x:Type DataGridCell}">
<EventSetter Event="UIElement.PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Yellow" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
Upvotes: 0
Views: 935
Reputation: 2469
Override Equals because it's likely your item isn't actually the same actual instance as the one in your items collection, so the binding doesn't work. Snoop will show the same values, so you may think its the same, when it really isn't. Put this in your class defining the object, replacing MyClasswith your class type etc.
public override bool Equals(object obj)
{
if (obj == null || !(obj is MyClass))
return false;
return ((MyClass)obj).Id == this.Id);
}
More info: https://rachel53461.wordpress.com/2011/08/20/comboboxs-selecteditem-not-displaying/#comments
Why is WPF ComboBox item not updating?
Upvotes: 1