They looked at me in amazement, “Really?”. Apparently that is a little known fact about WPF. Yes, it is absolutely possible to write inline code inside a XAML page without the need for a code-behind file, and everything will work as expected. I quickly realized that his was going somewhere I didn’t want it to go. I knew what was coming next; they asked me to show them how to do it. Uggg…
Okay here is the deal; what I am about to show you is not recommended. It goes against everything I have been teaching in the community, but if you are curious how to actually do this, it is quit simple.
<Grid>
<!–Define the button content –>
<Button Width="133" Height="24" Name="btnExitApp" Click="btnExitApp_Clicked" Content="Exit Application"/>
<!–The C# implementation of the button's Click event handler. –>
<x:Code>
<![CDATA[
private void btnExitApp_Clicked(object sender, RoutedEventArgs e)
{
// Get a handle to the current app and shut it down.
Application.Current.Shutdown();
}
]]>
</x:Code>
</Grid>
Read more: <elegantc*de>