wpf的mvvmlight控件事件绑定
先use命名空间
xmlns:intr="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
然后在需要绑定事件的控件上加上(ListViewLoadedCommand是绑定命令,lvmenu是绑定的元素)
<intr:Interaction.Triggers> <intr:EventTrigger EventName="Loaded"> <intr:InvokeCommandAction Command="{Binding Path=ListViewLoadedCommand}" CommandParameter="{Binding ElementName=lvmenu}"> </intr:InvokeCommandAction> </intr:EventTrigger> </intr:Interaction.Triggers>
后台代码
#region 事件绑定 private RelayCommand<ListView> _listViewLoadedCommand; /// <summary> /// 菜单点击事件 /// </summary> public RelayCommand<ListView> ListViewLoadedCommand { get { if (_listViewLoadedCommand == null) { _listViewLoadedCommand = new RelayCommand<ListView>((lv) => ShowForm(lv)); } return _listViewLoadedCommand; } set { _listViewLoadedCommand = value; } } //显示菜单 private void ShowForm(ListView lv) { _lvmeun = (ListView)lv; //_lvmeun.MouseDown += lvmeun_MouseDown; _lvmeun.PreviewMouseDoubleClick += _lvmeun_PreviewMouseDoubleClick; } private void _lvmeun_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e) { e.Handled = true; var lv = (ListView)sender; if (lv.SelectedItem != null) { MessageBox.Show(((SystemMeun)lv.SelectedItem).name); } } #endregion