【问题标题】:Attach method on ViewModel to events in WPF将 ViewModel 上的方法附加到 WPF 中的事件
【发布时间】:2009-09-01 11:11:06
【问题描述】:

如何将 WPF 控件上的事件绑定到我的 ViewModel 上的方法?

我有一个 ViewModel:

class MyViewModel {
    public string MyText { get; set; }
    public void MyMouseHandleMethod(object sender, EventArgs e) { }
}

在我的 DataTemplate 中:

<TextBlock Text="{Binding Text}">

现在我想将 ViewModel 上的一个方法附加到 TextBlock,例如:

<TextBlock Text="{Binding MyText}" MouseUp="{Binding MyMouseHandleMethod}">

如果不在代码隐藏中创建回调,我无法弄清楚如何做到这一点。

【问题讨论】:

    标签: wpf events mvvm


    【解决方案1】:

    研究使用来自here 的 AttachedCommandBehavior。它允许您完全在 XMAL 中将命令绑定到事件。不完全是你想要的,但它会给你同样的结果。

    【讨论】:

      【解决方案2】:

      我有一个新的解决方案,可以在 .NET 4.5+ 中使用事件的自定义标记扩展。它可以与多个参数、绑定和其他扩展一起使用,以提供参数值等。我在这里详细解释:

      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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-02-06
        • 2017-04-07
        • 2011-06-21
        • 2010-10-22
        • 1970-01-01
        • 1970-01-01
        • 2011-12-14
        相关资源
        最近更新 更多