I've got a DataGrid which contains a DataGridComboBoxColumn. What I want to do is to (dis)enable other columns based on the value of this combobox. For that, I handle the SelectionChanged event via an ICommand, but this command is never called. It works fine for comboboxes outside a DataGrid, but what is the trick inside a DataGrid ?
First remark, the value of Options is set only when the row is no more in edition mode.
Second remark, when enter key is pressed to end editing, the Options is not set even if it has been changed with the ComboBox
<DataGrid Grid.Row="0" AutoGenerateColumns="False"
ItemsSource="{Binding Path=AcquisitionList, Mode=TwoWay}"
SelectedItem="{Binding SelectedParameters}"
Margin="0,20,0,0" Name="dataGridAcquisitions"
CanUserAddRows="True" CanUserDeleteRows="True"
CanUserReorderColumns="False" SelectionUnit="FullRow">
<DataGridComboBoxColumn Header="Choice1" Width="SizeToHeader"
SelectedItemBinding="{Binding Options}"
ItemsSource="{Binding Source={StaticResource OptionValues}}"
EditingElementStyle="{StaticResource StandardComboBox}"
ElementStyle="{StaticResource StandardComboBox}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding EnableCustomParameters}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGridComboBoxColumn>
<DataGridComboBoxColumn Header="Choice2" Width="SizeToHeader"
SelectedItemBinding="{Binding FilterType}"
ItemsSource="{Binding Source={StaticResource FilterTypes}}"
EditingElementStyle="{StaticResource StandardComboBox}"
ElementStyle="{StaticResource StandardComboBox}" >
<DataGridComboBoxColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="IsEnabled" Value="{Binding CustomParametersEnabled}"/>
</Style>
</DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>
</DataGrid>
In the VM
private RelayCommand enableCustomParameters;
private Model.AcquisitionParameters selectedParameters;
private ObservableCollection<Model.AcquisitionParameters> acquisitionList = new ObservableCollection<Model.AcquisitionParameters>();
public ObservableCollection<Model.AcquisitionParameters> AcquisitionList
{
get { return acquisitionList; }
set { acquisitionList = value; OnPropertyChanged("AcquisitionList"); }
}
public Model.AcquisitionParameters SelectedParameters
{
get { return selectedParameters; }
set { selectedParameters = value; OnPropertyChanged("SelectedParameters"); }
}
public ICommand EnableCustomParameters
{
get
{
if(this.enableCustomParameters == null)
{
this.enableCustomParameters = new RelayCommand(
param => this.ChangeCustomState());
}
return this.enableCustomParameters;
}
}
public void ChangeCustomGainState()
{
SelectedParameters.CustomGainParametersEnabled = SelectedParameters.GainModeOptions == GainModeOptions.Custom;
}
And the Model
public class AcquisitionParameters : INotifyPropertyChanged
{
private Choice1 options;
private Choice2 filterType;
private bool customParametersEnabled;
public Choice1 Options
{
get { return options; }
set { options= value; OnPropertyChanged("Options"); }
}
public Choice2 FilterType
{
get { return filterType; }
set { filterType= value; OnPropertyChanged("FilterType"); }
}
public bool CustomParametersEnabled
{
get { return customParametersEnabled; }
set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
}
}