MVVM模式似呼控件的事件都成了摆设,百度了一个方法,没经过测试,先行记录下来,以后有需要再回来翻看
你的前端,首先需要引入touchMvvm的命名空间,然后DataGridActionsCommand能够连接你的VM即可。
<DataGrid touchMvvm:EventCommands.Events="{Binding DataGridActionsCommand}"/>
public class ViewModel : ViewModelBase
{
public ViewModel()
{
this.DataGridActionsCommand = new IEventAction[]
{
new EventAction<MouseButtonEventArgs>("MouseDoubleClick", DataGridMouseDoubleClick) ,
new EventAction<object,SelectionChangedEventArgs>("SelectionChanged", DataGridSelectionChanged)
};
}
public IEventAction[] DataGridActionsCommand { get; set; }
private void DataGridMouseDoubleClick(MouseButtonEventArgs e)
{
}
private void DataGridSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is DataGrid dataGrid)
{
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
namespace TouchMvvm
{
public interface IEventAction
{
string EventName { get; }
}
public static class EventCommands
{
// Using a DependencyProperty as the backing store for Events. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EventsProperty =
DependencyProperty.RegisterAttached("Events", typeof(IEnumerable<IEventAction>), typeof(EventCommands), new PropertyMetadata(null, OnCommandChanged));
public static IEnumerable<IEventAction> GetEvents(DependencyObject obj)
{
return (IEnumerable<IEventAction>)obj.GetValue(EventsProperty);
}
public static void SetEvents(DependencyObject obj, IEnumerable<IEventAction> value)
{
obj.SetValue(EventsProperty, value);
}
private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is IEnumerable eventActions)
{
foreach (IEventAction eventAction in eventActions)
{
if (!string.IsNullOrEmpty(eventAction.EventName))
{
EventInfo eventInfo = d.GetType().GetEvent(eventAction.EventName);
if (eventInfo == null)
{
throw new Exception($"没有找到名称为{eventAction.EventName}的事件");
}
Delegate @delegate = Delegate.CreateDelegate(eventInfo.EventHandlerType, eventAction, "Event");
//Delegate @delegate2 = eventAction.Begin(eventInfo.EventHandlerType, typeof(object), typeof(MouseButtonEventArgs));
//Delegate @delegate = DelegateBuilder.CreateDelegate(eventAction, "Event", eventInfo.EventHandlerType, BindingFlags.NonPublic);
eventInfo.AddEventHandler(d, @delegate);
}
else
{
throw new Exception($"事件名不能为空");
}
}
}
}
}
public class EventAction<TSender, TE> : IEventAction
{
private readonly Action<TSender, TE> action;
private readonly string eventName;
public EventAction(string eventName, Action<TSender, TE> action)
{
this.eventName = eventName;
this.action = action;
}
public string EventName => this.eventName;
private void Event(TSender sender, TE e)
{
this.action?.Invoke(sender, e);
}
}
public class EventAction<TE> : IEventAction
{
private readonly Action<object> action;
private readonly Action<TE> action2;
private readonly string eventName;
public EventAction(string eventName, Action<object> action)
{
this.eventName = eventName;
this.action = action;
}
public EventAction(string eventName, Action<TE> action)
{
this.eventName = eventName;
this.action2 = action;
}
public string EventName => this.eventName;
private void Event(object sender, TE e)
{
this.action?.Invoke(sender);
this.action2?.Invoke(e);
}
}
}
- 本文标题: WPF 之MVVM模式如何调用控件的事件
- 文章分类:【WinForm/WPF】
- 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.