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

89427412 2020-02-13

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

相关推荐

87281248 / 0评论 2013-03-27