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

猜你喜欢

转载自www.cnblogs.com/akiva/p/12303478.html