0
votes

I have a DatagridTemplateColumn with Combobox which is editable. Depending on another property value, this will be either a Text box or combo box. So, if Modelist is null, isEditable will be true making it more like a text box and if list is initialized isEditable will be false with dropdown values. So as user edit, the SelectedModeValue gets updated.

My problem is, say when isEditable is false, the user chooses one item from the dropdown, the SelectedModeValue will have that chosen value, say "mode1". Now, on changing someother property( say 'ModeType' from code below), my ModeList will go null and isEditable becomes True as there are no list. TextBox is displayed correctly with no default value, but behind the SelectedModeValue still has value "mode1". it needs to be resets to be string.empty. Hence, i wanted to know if there is any event when isEditable changes value on the combo box. If thats possible, with that event can you show me how i can reset SelectedModeValue depending on the isEditable change event? Or any other alternative way ?

<DataGrid.Columns>
<DataGridTemplateColumn Header="ModeList">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ComboBox ItemsSource="{Binding ModeList}" SelectionChanged="ValueChanged"
                                              Text="{Binding SelectedModeValue, Mode=TwoWay, UpdateSourceTrigger=LostFocus, ValidatesOnNotifyDataErrors=True, 
                                                ValidatesOnDataErrors=True,NotifyOnValidationError=True, ValidatesOnExceptions=True}">
                                        <ComboBox.Style>
                                            <Style TargetType="ComboBox">
                                                <Setter Property="IsEditable" Value="False"/>
                                                <Style.Triggers>
                                                    <DataTrigger Binding="{Binding ModeList}" Value="{x:Null}">
                                                        <Setter Property="IsEditable" Value="True" />
                                                    </DataTrigger>
                                                </Style.Triggers>
                                            </Style>
                                        </ComboBox.Style>
                                    </ComboBox>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
...<! other properties -->
</DataGrid.Columns>
public List<string> ModeList
        {
            get { return modeList; }
            set
            {
                modeList= value;

                OnPropertyChanged("ModeList");
            }
        }

public ModeTypeEnum ModeType
        {
            get { return modeType; }
            set
            {
                modeType = value;
                OnPropertyChanged("ModeType");
            }
        }

    public enum ModeTypeEnum  
    {
        slow= 0,
        Fast = 1,
        Forward = 2,
    }
1

1 Answers

0
votes

You should handle this in the setter of the property in the data object:

public List<string> ModeList
{
    get { return modeList; }
    set
    {
        modeList = value;

        OnPropertyChanged("ModeList");

        if (modeList == null)
            SelectedModeValue = string.Empty:
    }
}

Implementing this kind of logic in the view is a bad idea, especially if you adopt to the MVVM design pattern.