3

WPF 没有定义用于事件的标记扩展,第三方能够创建可用于事件的标记扩展。现在 WPF 4.5 支持事件的标记扩展。任何人都可以通过一个优雅的例子帮助如何在.Net 4.5中实现这一点吗?

4

3 回答 3

5

事件标记扩展允许您对事件使用标记扩展,在 WPF 4.5 之前,它们仅可用于属性。例如:

<Canvas ClipToBounds="True" Background="White"
        MouseLeftButtonDown="{local:EventToCommand StartPaintCommand}"
        MouseMove="{local:EventToCommand AddLineCommand}"
        MouseLeftButtonUp="{local:EventToCommand EndPaintCommand}">
</Canvas>

一个完整的例子可以在这里找到。

于 2013-06-20T20:30:57.873 回答
1

命令

{eb:EventBinding}(查找命令的简单命名模式)

{eb:EventBinding 命令=命令名称}

命令参数

$e (EventAgrs)

$this 或 $this.Property

细绳

https://github.com/JonghoL/EventBindingMarkup

于 2014-11-20T02:06:19.783 回答
0

这是我编写的一个非常通用的标记扩展的示例,它可以将事件直接绑定到视图模型上的方法:

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);
}
于 2016-07-05T18:17:48.620 回答