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:
<Grid>
QR:
- 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[
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");
}
{
Button btn = sender as Button;
MessageBox.Show("Button is clicked","Inside x:Code");
}
]]>
</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.
QR:
0 comments:
Post a Comment