WPF does not define a markup extension to be used for events, third parties are able to create a markup extension that can be used with events. Now WPF 4.5 supports markup extensions for events. Can anyone help how to achieve this in .Net 4.5 with an elegant example?
Asked
Active
Viewed 1,517 times
3
-
What exactly are you trying to accomplish? Registering an event handler in XAML? – Justin Niessner Jun 20 '13 at 20:11
-
Justin - thanks for your fast response.. http://msdn.microsoft.com/en-us/library/bb613588.aspx#events_markup_extenions i have found a new feature and searched for some good samples and didn't get any samples – Vimal CK Jun 20 '13 at 20:15
-
@VimalCk, I have been working on this also. Just to mention that 4.5 also lets you use generics in the ways to reflect properties, and this speeds up the markup extension a whole lot! – Gayot Fow Jun 20 '13 at 21:14
3 Answers
5
Event markup extensions allow you to use markup extensions for events, where up until WPF 4.5 they were only available for properties. For example:
<Canvas ClipToBounds="True" Background="White"
MouseLeftButtonDown="{local:EventToCommand StartPaintCommand}"
MouseMove="{local:EventToCommand AddLineCommand}"
MouseLeftButtonUp="{local:EventToCommand EndPaintCommand}">
</Canvas>
A complete example can be found here.

Adi Lester
- 24,731
- 12
- 95
- 110
-
Beautiful. If only Microsoft continued to put money in WPF... I just hope all these features will ever be available in WinRT... or whatever other half-baked framework they come up with. – Federico Berasategui Jun 20 '13 at 20:48
-
@Adi, Thanks buddy, i have tested the application and its working fine – Vimal CK Jun 20 '13 at 22:06
1
Command
{eb:EventBinding} (Simple naming pattern to find Command)
{eb:EventBinding Command=CommandName}
CommandParameter
$e (EventAgrs)
$this or $this.Property
string

jongho
- 79
- 2
-
Nice. This was a killer feature missing from WPF. Much of Microsoft's tech. makes no sense to me. Not the least of which is this arbitrary distinction between commands and events. UI's are simply an event source, but the fail to provide a way to reliable signal it's plethora of events back to the view model without resorting to code behind. Uh? Their eventual solution was so verbose and required a third library...Uh? ... – George Jul 20 '15 at 20:31
0
Here is an example of a very versatile markup extension I wrote that can bind events directly to methods on your view model:
http://www.singulink.com/CodeIndex/post/building-the-ultimate-wpf-event-method-binding-extension
Usage:
<!-- 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}}" />
View model method signatures:
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);
}

Mike Marynowski
- 3,156
- 22
- 32