XAML 2009 Features: XAML Event handling

[This is part of a series on New WPF\XAML Features]  

In previous releases, when loose XAML had events in it and was loaded it would crash. In V4, there are 2 options to avoid this crash.

à On loading events are searched on the root object of the XAML file. Suppose you had the loose XAML like the below



        xmlns:local =”clr-namespace:DemoApp;assembly=DemoApp”>

    <Button Click=”Button_Click” >Date (Event on Root)</Button>


As long as the root object (in this case MyStackPanel) can be found, the event will be hooked and will work fine without a crash

à The second approach is to define a MarkupExtension that returns a delegate and assign this to the event. So you could have XAML like the below


<Button Click=”{DelegateCreatingME}”/>

<Button Click=”{DelegateCreatingME ChangeFontSize}” >

<Button Click=”{DelegateCreatingME ChangeForeground,Brown}” />



The sample project using the CoreMVVM library is attached. 

 Note that XAML 2009 features work only for loose XAML in WPF.

Share this post



Comments (0)