c# - MVVM 中的 DataGridComboBoxColumn SelectionChanged 事件

标签 c# wpf mvvm datagrid

我有一个包含 DataGridComboBoxColumn 的 DataGrid。我想要做的是根据此组合框的值(禁用)启用其他列。为此,我通过 ICommand 处理 SelectionChanged 事件,但从未调用此命令。它适用于 DataGrid 外部的组合框,但 DataGrid 内部的技巧是什么?

首先要注意的是,仅当该行不再处于编辑模式时才设置 Options 的值。

第二点,当按回车键结束编辑时,即使已使用组合框更改选项也未设置

 <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>

在虚拟机中

  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"); }
  }
}

最佳答案

您应该使用适当的绑定(bind):

{Binding Path=yourCommandName, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}

here 阅读更多关于不同绑定(bind)的信息.

关于c# - MVVM 中的 DataGridComboBoxColumn SelectionChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549275/

相关文章:

c# - MeshRenderer 和 Renderer 组件的区别

c# - ElasticSearch从指定术语中查找具有不同嵌套列表元素的索引对象

C# 多维不可变数组

c# - WPF MVVM ContextMenu 绑定(bind) IsOpen 到模型

c# - CefSharp 与 WPF MVVM

c# - C# 逻辑树的持久化

c# - 将 KeyBinding 命令绑定(bind)到 WPF DatePicker (MVVM)

WPF 选择哪个 IDE?

c# - Entity Framework - 将组合框绑定(bind)到规范化表字段

c# - 具有启用虚拟化功能的WPF DataGrid Multiselect MVVM