Syncfusion WPF FAQ
Questions and answers in this FAQ have been collected from newsgroup posts, various mailing lists and the employees of Syncfusion.

5. Concepts Coding Techniques

FAQ Home
   5.1 What is the use of x:code ?
   5.2 How do I create in-line event handlers?



5.1 What is the use of x:code ?


x:Code is a directive element defined in XAML. An x:Code directive element can contain inline programming code. The code that is defined inline can interact with the XAML on the same page. The following example illustrates inline C# code. Notice that the code is inside the x:Code element and that the code must be surrounded by <CDATA[...]]> to escape the contents for XML, so that an XAML processor (interpreting either the XAML schema or the WPF schema) will not try to interpret the contents literally as XML.

[XAML]

<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyNamespace.MyCanvasCodeInline"
>
<Button Name="button1" Click="Clicked">Click Me!</Button>
<x:Code><![CDATA[
void Clicked(object sender, RoutedEventArgs e)
{
button1.Content = "Hello World";
}
]]></x:Code>
</Page>



5.2 How do I create in-line event handlers?


Here are some examples:
Example 1:


animation.Completed += delegate
{
// Start the other animation after the end of the previous animation.
index++;
if (lstDefinitions.Count > index)
this.PerformAnimations(index, lstDefinitions);
};


Example 2:


label.MouseDown += (sender, e) =>
{
Console.WriteLine("MouseDown:{0}", e.OriginalSource);

//Not clear why this is needed, but I guess this can effectively bypass
// any attempt from the parent control to capture the mouse inside MouseDown handler.
e.Handled = true;
};



© 2001-2010 Copyright Syncfusion Inc. All rights reserved.  |  Privacy Policy  |  Contact  |  Sitemap