I have a GridView
whith DataGridComboBoxColumn
, it works fine the first time, but when set the column visibility
to collapsed
, and then return the visibility
to visible
again, the column lose its value. but the property which is bounded to it still has the correct value.
when refresh the DataContext all the values gets binded correctly.
I add a test Converter, and noticed that when collapsing the column, the call back method is fired, and the value is null.
<DataGridComboBoxColumn SelectedValueBinding="{Binding DbId}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
Visibility="Visible">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding TestList}"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding TestList}"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
I can reproduce your issue. You could work around it by handling the Loaded
event for the ComboBox
in the CellTemplate
and re-set the SelectedValue
property:
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding TestList}"/>
<EventSetter Event="Loaded" Handler="OnLoaded" />
</Style>
</DataGridComboBoxColumn.ElementStyle>
private void OnLoaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
dynamic dataObject = cmb.DataContext;
cmb.SelectedValue = dataObject.DbId;
}
Another solution would be to replace the DataGridComboBoxColumn
with a DataGridTemplateColumn
. You then want to bind the SelectedItem
property of the ComboBox
to a Db
property of your data object to be able to display the Name
property of the Db
object in the CellTemplate
.