【问题标题】:DataGridComboBoxColumn SelectionChanged event in MVVMMVVM 中的 DataGridComboBoxColumn SelectionChanged 事件
【发布时间】:2015-04-17 10:15:10
【问题描述】:

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

首先说明,Options的值仅在该行不再处于编辑模式时设置。

第二个说明,当按下回车键结束编辑时,即使用ComboBox改变了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"); }
  }
}

【问题讨论】:

标签: c# wpf mvvm datagrid


【解决方案1】:

这并不是真正的解决方案,而是一种解决方法。由于我没有找到在组合框项目更改时触发事件的方法,因此我使用了属性 Options。

首先,我必须使用属性“UpdateSourceTrigger=PropertyChanged”来装饰 SelectedItemBinding,该属性指定要更新的绑定源的时间。从文档中,PropertyChanged “只要绑定目标属性发生更改,就会立即更新绑定源。”这正是我正在寻找的行为。

然后,模型被修改如下:

public class AcquisitionParameters : INotifyPropertyChanged
{
  private Choice1 options;
  private Choice2 filterType;
  private bool customParametersEnabled;

  public Choice1 Options
  {
     get { return options; }
     set 
     { 
        options= value; 
        CustomParametersEnabled = options == Choice1.Options1;
        OnPropertyChanged("Options"); 
        OnPropertyChanged("CustomParametersEnabled "); 
     }
  }

  public Choice2 FilterType
  {
     get { return filterType; }
     set { filterType= value; OnPropertyChanged("FilterType"); }
  }

  public bool CustomParametersEnabled
  {
     get { return customParametersEnabled; }
     set { customParametersEnabled = value; OnPropertyChanged("CustomParametersEnabled"); }
  }
}

然后,当参数 Options 的第一个 ComboBox 的选定项发生更改时,立即设置属性 Options,并且我也更新了 CustomParametersEnabled。这不是我第一次尝试时想做的方式,但它确实有效。

【讨论】:

    【解决方案2】:

    您应该使用适当的绑定:

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

    here 阅读有关不同绑定的更多信息。

    【讨论】:

    • 感谢您的回复,但它也不起作用。我之前尝试过这个解决方案但没有成功。我试图绑定到 DataGrid 或 UserControl 祖先,但它失败了。我对 Choice1 ComboBox 的 ItemsSource 有同样的问题。为此,解决方法是创建一个静态资源,因为选择列表不会更改并且是控件的属性。
    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 2012-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多