Live Chat Icon For mobile
Live Chat Icon

WPF FAQ - Coding Techniques

Find answers for the most frequently asked questions
Expand All Collapse All

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>
Permalink

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

Permalink

Share with

Share on twitter
Share on facebook
Share on linkedin

Couldn't find the FAQs you're looking for?

Please submit your question and answer.