MVVM中的DataGridComboBoxColumn SelectionChanged事件

时间:2015-02-16 19:51:31

标签: c# wpf mvvm datagrid

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

首先,只有当行不再处于编辑模式时,才会设置选项的值。

第二个注释,当按下回车键结束编辑时,即使用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>

在虚拟机中

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

2 个答案:

答案 0 :(得分:0)

您应该使用适当的绑定:

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

详细了解here的不同绑定。

答案 1 :(得分:0)

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

首先,我必须用属性&#34; UpdateSourceTrigger = PropertyChanged&#34;来装饰SelectedItemBinding。指定要更新的绑定源的时间。从doc,PropertyChanged&#34; 每当绑定目标属性发生更改时立即更新绑定源。&#34;这正是我所寻找的行为。

然后,模型修改如下:

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。这不是我第一次尝试的方式,但它确实有效。

相关问题