У меня есть DataGrid, который содержит DataGridComboBoxColumn. То, что я хочу сделать, - это (dis) включить другие столбцы на основе значения этого списка. Для этого я обрабатываю событие SelectionChanged через ICommand, но эта команда никогда не вызывается. Он отлично работает для comboboxes вне DataGrid, но что такое трюк внутри DataGrid?DataGridComboBoxColumn SelectionChanged событие в MVVM
Первое примечание: значение параметра задается только тогда, когда строка больше не находится в режиме редактирования.
Второе замечание, когда клавишу ввода нажимается для завершения редактирования, параметры не установлены, даже если он был изменен с 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>
В 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;
}
И модель
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"); }
}
}
Я думаю, что это может помочь: http://stackoverflow.com/questions/19322271/wpf-datagrid-combobox-column-how-to-manage-event-of-selection-changed BR, Daniel – dba