Here, we will learn how to write C# code in WPF XAML.
Step 1 Create WPF Window
- Create new WPF window in WPF application.
- Add a Button in the created window.
- <Window x:Class="DropBoxIntegration.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DropBoxIntegration" mc:Ignorable="d" Title="MainWindow" Height="321" Width="1024" MinHeight="300" MinWidth="600">
- <Grid>
- <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="273,3,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="17">
-
- </Button> </Grid>
- </Window>
Step 2 Messagebox popup on button click.
- Add the below mentioned code as it is inside <button> tag.
- Bind the Buttonclick event to the below event “Clicked”.
- <x:Code>
- <![CDATA[
-
- void Clicked(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Hello World");
- }
- ]]>
- </x:Code>
- The final status will be something like mentioned below.
- <Window x:Class="DropBoxIntegration.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:DropBoxIntegration" mc:Ignorable="d" Title="MainWindow" Height="321" Width="1024" MinHeight="300" MinWidth="600">
- <Grid>
- <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="273,3,0,0" Grid.Row="3" VerticalAlignment="Top" Width="75" Height="17" Click="Clicked">
-
- <x:Code>
- <![CDATA[
- void Clicked(object sender, RoutedEventArgs e)
- {
- MessageBox.Show("Hello World");
- }
- ]]>
- </x:Code>
- </Button> </Grid>
- </Window>
That’s it. We are done. Try it yourself and see the result.