XAML 绑定到ViewModel的WPF事件(对于非Command类)

ep6jt1vc  于 2023-01-10  发布在  其他
关注(0)|答案(5)|浏览(298)

我正在开发一个应用程序的第二个版本,作为重写的一部分,我必须转移到MVVM架构。我正承受着把每一位代码都放在视图模型类中的压力--在代码隐藏文件中使用c#是不受欢迎的。(我知道,我知道......我明白代码隐藏不是一件坏事,但这次不是我说了算)。
对于实现命令接口的对象来说,这很容易。我已经找到了大量关于如何将这些对象的命令绑定到视图模型中的ICommand的信息。问题是对于没有这个接口的对象,例如。

<ListBox
   x:Name="myListBox"
   MouseDoubleClick="myCallbackFunction">

<!-- ... -->

</ListBox>

我想知道如何将列表框的MouseDoubleClick事件绑定到视图模型中实现的myCallbackFunction,这可能吗?
谢谢!

anauzrmj

anauzrmj1#

这是不可能直接实现的,可以通过Attached Property或Behavior来实现,尽管找到并调用合适的方法仍然有点棘手(这可以通过Reflection相当容易地实现)。
也就是说,这通常是通过ICommand处理的-例如,MVVM Light有一个很好的EventToCommand行为,可以将任何事件Map到ViewModel上的ICommand。使用ICommand的好处是,您仍然可以使用DataBinding,因为ICommand是作为属性公开的。

a0x5cqrl

a0x5cqrl2#

从.NET 4.5开始,WPF支持事件的标记扩展。使用该功能,我实现了一个多功能的方法绑定扩展,并在此处介绍了它:
http://www.singulink.com/CodeIndex/post/building-the-ultimate-wpf-event-method-binding-extension
可用于绑定到使用完整属性路径语法的方法,支持绑定和其他标记扩展作为参数,并自动路由到与所提供参数的签名匹配的方法。下面是一些用法示例:

<!--  Basic usage  -->
<Button Click="{data:MethodBinding OpenFromFile}" Content="Open" />

<!--  Pass in a binding as a method argument  -->
<Button Click="{data:MethodBinding Save, {Binding CurrentItem}}" Content="Save" />

<!--  Another example of a binding, but this time to a property on another element  -->
<ComboBox x:Name="ExistingItems" ItemsSource="{Binding ExistingItems}" />
<Button Click="{data:MethodBinding Edit, {Binding SelectedItem, ElementName=ExistingItems}}" />

<!--  Pass in a hard-coded method argument, XAML string automatically converted to the proper type  -->
<ToggleButton Checked="{data:MethodBinding SetWebServiceState, True}"
                Content="Web Service"
                Unchecked="{data:MethodBinding SetWebServiceState, False}" />
                
<!--  Pass in sender, and match method signature automatically -->
<Canvas PreviewMouseDown="{data:MethodBinding SetCurrentElement, {data:EventSender}, ThrowOnMethodMissing=False}">
    <controls:DesignerElementTypeA />
    <controls:DesignerElementTypeB />
    <controls:DesignerElementTypeC />
</Canvas>

    <!--  Pass in EventArgs  -->
<Canvas MouseDown="{data:MethodBinding StartDrawing, {data:EventArgs}}"
        MouseMove="{data:MethodBinding AddDrawingPoint, {data:EventArgs}}"
        MouseUp="{data:MethodBinding EndDrawing, {data:EventArgs}}" />

<!-- Support binding to methods further in a property path -->
<Button Content="SaveDocument" Click="{data:MethodBinding CurrentDocument.DocumentService.Save, {Binding CurrentDocument}}" />

查看模型方法签名:

public void OpenFromFile();
public void Save(DocumentModel model);
public void Edit(DocumentModel model);

public void SetWebServiceState(bool state);

public void SetCurrentElement(DesignerElementTypeA element);
public void SetCurrentElement(DesignerElementTypeB element);
public void SetCurrentElement(DesignerElementTypeC element);

public void StartDrawing(MouseEventArgs e);
public void AddDrawingPoint(MouseEventArgs e);
public void EndDrawing(MouseEventArgs e);

public class Document
{
    // Fetches the document service for handling this document
    public DocumentService DocumentService { get; }
}

public class DocumentService
{
    public void Save(Document document);
}
xoefb8l8

xoefb8l83#

要直接回答您的问题,请参阅Why to avoid the codebehind in WPF MVVM pattern?它建议您可能需要的两个东西。

但是,为什么要将ListBox的MouseDoubleClick绑定到视图模型中的ICommand?

另一种方法是在代码隐藏中编写一个方法来注册MouseDoubleClick。由于以下事实,这并不坏。
1.有意义的数据绑定是视图和视图模型之间的交互。例如,当用户向TextBox输入一些文本时,视图模型也会更新。相反,如果视图模型从数据库获取数据,则该数据将显示在视图中。但是,视图模型中的ICommand与视图绑定的情况并非如此。
1.当然,ICommand的CanExecute对视图模型很重要,但在许多情况下,它与视图模型无关或不受关注。在这种情况下,ICommand绑定和编写代码隐藏之间的区别在于MouseDoubleClick事件与ICommand绑定或向事件处理程序注册的位置。

e7arh2l6

e7arh2l64#

一种方法是在代码后处理事件,并从代码后调用视图模型的适当方法
你也可以去一些现成的命令库,如this教程使用ACB

cetgtptt

cetgtptt5#

尝试EventBinder,它允许您将方法直接绑定到任何事件,包括您自己的事件,而无需将方法 Package 在ICommand容器中。
https://github.com/Serg046/EventBinder
https://www.nuget.org/packages/EventBinder
支持. NET框架3.0+、. NET核心3.0+和Avalonia。
特点:

  • 绑定到不带ICommand的方法
  • 绑定到具有返回类型的方法
  • 绑定到异步方法
  • 支持使用.分隔符、属性和字段绑定到嵌套对象
  • 传递int、double、decimal或string类型的用户参数
  • 使用$符号和位置编号($0$1等)传递事件参数
  • 将默认值{Binding}作为参数传递

用法:

public class ViewModel
{
    public MetadataViewModel Metadata { get; } = new MetadataViewModel();

    public async Task ShowMessage(string msg, decimal centenary, double year)
    {
        await Task.Delay(0);
        MessageBox.Show(msg + centenary + year);
    }

    public class MetadataViewModel
    {
        public void ShowInfo(Window window, double windowWidth, ViewModel viewModel, object sender, MouseButtonEventArgs eventArgs)
        {
            var sb = new StringBuilder("Window width: ")
                .AppendLine(windowWidth.ToString())
                .Append("View model type: ").AppendLine(viewModel.GetType().Name)
                .Append("Sender type: ").AppendLine(sender.GetType().Name)
                .Append("Clicked button: ").AppendLine(eventArgs.ChangedButton.ToString())
                .Append("Mouse X: ").AppendLine(eventArgs.GetPosition(window).X.ToString())
                .Append("Mouse Y: ").AppendLine(eventArgs.GetPosition(window).Y.ToString());
            MessageBox.Show(sb.ToString());
        }
    }
}

装订:

<Window xmlns:e="clr-namespace:EventBinder;assembly=EventBinder" Name="Wnd">
    <Rectangle Fill="LightGray" Name="Rct"
        MouseLeftButtonDown="{e:EventBinding ShowMessage, `Happy `, 20m, 20.0 }"
        MouseRightButtonDown="{e:EventBinding Metadata.ShowInfo, {Binding ElementName=Wnd},
            {Binding ElementName=Wnd, Path=ActualWidth}, {Binding}, $0, $1 }" />
</Window>

EventBinding.Bind(Rct, nameof(Rct.MouseLeftButtonDown),
    nameof(ViewModel.ShowMessage),
    "`Happy `", 20m, 20.0);
EventBinding.Bind(Rct, nameof(Rct.MouseRightButtonDown),
    nameof(ViewModel.Metadata) + "." + nameof(ViewModel.Metadata.ShowInfo),
    new Binding { ElementName = nameof(Wnd)},
    new Binding("ActualWidth") { ElementName = nameof(Wnd) },
    new Binding(),
    "$0", "$1");

相关问题