码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2020-02-13 15:10:04      阅读:300      评论:0      收藏:0      [点我收藏+]

标签:binding   gen   end   foreach   round   object   lte   cas   relative   

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.

标签:binding   gen   end   foreach   round   object   lte   cas   relative   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!