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

时间:2020-02-13 15:25:31   收藏:0   阅读:439

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

原文:https://www.cnblogs.com/akiva/p/12303478.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!