1

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>
IBRA
  • 1,502
  • 3
  • 24
  • 56
  • you will need to implement INotifyPropertyChanged on TestList so that the UI will reflect the change. – CodeConstruct Jul 12 '17 at 13:10
  • already implemented and working fine, updating the datacontext gets all the values updated correctly. – IBRA Jul 12 '17 at 13:12
  • can you post more code details? converter i mean – CodeConstruct Jul 12 '17 at 13:21
  • the converter does not have any logic, it just to check the values. when collapsing the column, the convert back method is called, and the value is null. – IBRA Jul 12 '17 at 13:28
  • return the same visibility which you are returning in convert method. – CodeConstruct Jul 12 '17 at 13:36
  • the problem is with the selected value, not the visibility, it's not bounding correctly after setting the column from collapsed to visible. – IBRA Jul 12 '17 at 13:42

1 Answers1

1

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.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • on Loaded I just called the DataContext and raised OnPropertyCanged and it works fine. thanks – IBRA Jul 13 '17 at 05:52