<Window x:Class="WpfApp71.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp71" mc:Ignorable="d" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:interaction="http://schemas.microsoft.com/expression/2010/interactions" Title="MainWindow" Height="450" Width="800"> <Grid> <DataGrid x:Name="dg" Grid.Row="1" ItemsSource="{Binding BooksCollection,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderBrush="Blue" BorderThickness="3" SelectionMode="Extended" SelectedItem="{Binding SelectedBook,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" > <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <interaction:CallMethodAction TargetObject="{Binding}" MethodName="OnSelectionChanged"/> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp71 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MainVM vm = new MainVM(); this.DataContext = vm; } } public class Book { public int Id { get; set; } public string ISBN { get; set; } public string Name { get; set; } public string Summary { get; set; } public string Title { get; set; } public string Topic { get; set; } } public class ModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string propName) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propName)); } } } public class MainVM:ModelBase { public MainVM() { InitLV(); } private void InitLV() { BooksCollection = new ObservableCollection<Book>(); for (int i = 0; i < 1000; i++) { BooksCollection.Add(new Book() { Id = i + 1, ISBN = $"ISBN_{i + 1}", Name = $"Name_{i + 1}", Summary = $"Summary{i + 1}", Title = $"Title_{i + 1}", Topic = $"Topic{i + 1}", }); } } private ObservableCollection<Book> booksCollection; public ObservableCollection<Book> BooksCollection { get { return booksCollection; } set { if (value != booksCollection) { booksCollection = value; } } } public void OnSelectionChanged(object sender,SelectionChangedEventArgs e) { var dg = sender as DataGrid; var selectedItems = e.AddedItems; StringBuilder builder = new StringBuilder(); foreach(var item in selectedItems) { var bk= item as Book; if(bk!=null) { builder.AppendLine($"Id:{bk.Id},Name:{bk.Name}"); } } System.Windows.MessageBox.Show($"{builder.ToString()}", $"Selected {selectedItems.Count} items", MessageBoxButton.OK); } } }
Key part
1.Install System.Windows.Interactivity.WPF from nuget;
2.Add interactivity and interactivity in xaml
3.Pass event method to viewmodel via interaction:CallMethodAction and specified MethodName
<i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <interaction:CallMethodAction TargetObject="{Binding}" MethodName="OnSelectionChanged"/> </i:EventTrigger> </i:Interaction.Triggers>
4.Implemented the MethodName in viewmodel with same signature as event method in xaml.cs
public void OnSelectionChanged(object sender,SelectionChangedEventArgs e)
Be cautious,the Method's accessor must be public
标签:CallMethodAction,via,MethodName,get,Windows,System,set,using,public From: https://www.cnblogs.com/Fred1987/p/18165627