WPF MVVM ComboBox(in DataGrid)触发事件SelectionChanged, 通知到ViewModel.

Posted akiva

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF MVVM ComboBox(in DataGrid)触发事件SelectionChanged, 通知到ViewModel.相关的知识,希望对你有一定的参考价值。

1.View

<DataGrid>

  <DataGrid.Resources>

    <DataTemplate x:Key="ComputeRuleColumnTemplate">
      <ComboBox x:Name=‘ComputeRuleComboBox‘>
        <i:Interaction.Triggers>
          <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding DataContext.ComboBoxComputeRuleSelectionChangedCommand,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
          </i:EventTrigger>
        </i:Interaction.Triggers>

      </ComboBox>
    </DataTemplate>

  </DataGrid.Resources>

</DataGrid>

 

2.View.cs

private void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{

  foreach (var name in names)
  {

    switch (dataGridHeader)
    {
      case "COMPUTERULE":
        dataGridTemplateColumn = new DataGridTemplateColumn { Header = name };
        dt = dataGrid.Resources["ComputeRuleColumnTemplate"] as DataTemplate;
        dataGridTemplateColumn.CellTemplate = dt;
        dg.Columns.Add(dataGridTemplateColumn);
      break;

    }

  }

}

 

3.ViewModel

public ICommand ComboBoxComputeRuleSelectionChangedCommand { get; private set; }

public void InitialCommand()
{

  ComboBoxComputeRuleSelectionChangedCommand = new DelegateCommand(OnComboBoxComputeRuleSelectionChangedCommand);
}

private void OnComboBoxComputeRuleSelectionChangedCommand()
{
  UpdateResultObrclnByComboBoxComputeRuleSelectionChanged();
}

以上是关于WPF MVVM ComboBox(in DataGrid)触发事件SelectionChanged, 通知到ViewModel.的主要内容,如果未能解决你的问题,请参考以下文章

WPF DataGridTemplateColumn 与 ComboBox 绑定(MVVM 模式)

WPF MVVM 将 ComboBox 绑定到 Datagrid 选定项

MVVM WPF ComboBox SelectedValue 不是确切值

WPF MVVM ComboBox SelectedItem 或 SelectedValue 不起作用

WPF中ComboBox控件的SelectedItem和SelectedValue的MVVM绑定

MVVM WPF ComboBox SelectedItem 绑定未在数据网格内激活