This is a mirror of official site: http://jasper-net.blogspot.com/

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 :

    <Grid>
    <Button Margin="50" Width="300" Height="100"
                 Click="Button_click" Content="Click Me"/>
    <x:Code>
    <![CDATA[
    void Button_click(object sender, RoutedEventArgs args)
    {
    Button btn = sender as Button;
    MessageBox.Show("Button is clicked","Inside x:Code");
    }
    ]]>
    </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
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=http://dailydotnettips.com/2011/08/21/writing-inline-code-in-wpf/

Posted via email from Jasper-net

0 comments: