This is a mirror of official site:

Writing inline Code in WPF

| Monday, August 22, 2011
Generally when you create a WPF window, you will get two files automatically created for you. One which represents the XAML, which is stored in .XAML extension file, and another is a class which is stored in a Code-Behind file with .cs extension. But in certain scenarios, it is actually required to write your Code – behind directly in XAML using inline x:Code styles. This post will give you a short tip to publish inline code in WPF.

Steps to create Inline Code:

  • Create an application to start with and delete the class files from it.
  • We use x:Code to embed our code inside a XAML file. Lets add the code below to the XAML :

    <Button Margin="50" Width="300" Height="100"
                 Click="Button_click" Content="Click Me"/>
    void Button_click(object sender, RoutedEventArgs args)
    Button btn = sender as Button;
    MessageBox.Show("Button is clicked","Inside x:Code");

  • Remember to add the code inside ![CDATA[ ]] block. It is important.
  • Once you are done with it, execute the project and you will see the project runs fine.

Read more: Daily .Net Tips

Posted via email from Jasper-net