首页 WinForm/WPF WPF 之MVVM模式如何调用控件的事件

WPF 之MVVM模式如何调用控件的事件

作者:胡同里的砖头 围观群众:194 更新于:2023-10-10

MVVM模式似呼控件的事件都成了摆设,百度了一个方法,没经过测试,先行记录下来,以后有需要再回来翻看

你的前端,首先需要引入touchMvvm的命名空间,然后DataGridActionsCommand能够连接你的VM即可。

<DataGrid  touchMvvm:EventCommands.Events="{Binding DataGridActionsCommand}"/>

后端VM代码。
如果想要更多的事件,前端不用动,只需要增加对应事件的EventAction即可。
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】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8