Now the time has come to build your first WPF Application. To do this, let open Visual Studio 2008 / 2010. For this example I used Visual Studio 2008. Create a new Project.
You will see a new window.
The XAML will look like :
<Window x:Class="FirstWindowsApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window1"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</Window>
Here the blank window is produced. Height / Width represents the Size of the Window.
Title determines the text which is displayed in the TitleBar of the window.
Every control in XAML could be named with x:Name attribute. This will be used to reference the Window object in your XAML. x:Class attribute represents the class which should be associated with current Window. As I already told you, that XAML is not self sufficient, so to define logic you need a class in C# or VB.NET.
Grid is the primary layout for WPF application. Grid can take multiple Child elements.
So let us put some controls into the grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="50" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Enter Name :" Grid.Row="0" Grid.Column="0" />
<TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" MinWidth="50"/>
<Button Content="Click Me" Grid.Row="0" Grid.Column="2"
Click="Button_Click"/>
</Grid>You can see I have defined RowDefination and ColumnDefination. This allows you to divide the grid into cells so that you can place your control exactly where you want to. For each RowDefination and ColumnDefination you can use Height and Width of it.
You can see I have used 50, Auto, and * as width. Auto represents the size which we would define for the control during control definition. * indicates the rest of the space which it can take. Thus you can see the button is spread the rest of the area of the column it finds.
Now in the Behind I put a Messagebox to show the content of TextBox.
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("Hi {0}", this.txtName.Text));
}Therefore you can see your name will be prompted to you.
If you have noticed the XAML minutely, you might think how can I define the property of Grid within other controls. Like as I defined Grid.Row=0 in each of the control. This is actually possible due to the use of Dependency properties. Its a new feature introduced with WPF. We will discuss in detail later on.
You will see a new window.
The XAML will look like :
<Window x:Class="FirstWindowsApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window1"
Title="Window1" Height="300" Width="300">
<Grid>
</Grid>
</Window>
Here the blank window is produced. Height / Width represents the Size of the Window.
Title determines the text which is displayed in the TitleBar of the window.
Every control in XAML could be named with x:Name attribute. This will be used to reference the Window object in your XAML. x:Class attribute represents the class which should be associated with current Window. As I already told you, that XAML is not self sufficient, so to define logic you need a class in C# or VB.NET.
Grid is the primary layout for WPF application. Grid can take multiple Child elements.
So let us put some controls into the grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="50" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Enter Name :" Grid.Row="0" Grid.Column="0" />
<TextBox x:Name="txtName" Grid.Row="0" Grid.Column="1" MinWidth="50"/>
<Button Content="Click Me" Grid.Row="0" Grid.Column="2"
Click="Button_Click"/>
</Grid>You can see I have defined RowDefination and ColumnDefination. This allows you to divide the grid into cells so that you can place your control exactly where you want to. For each RowDefination and ColumnDefination you can use Height and Width of it.
You can see I have used 50, Auto, and * as width. Auto represents the size which we would define for the control during control definition. * indicates the rest of the space which it can take. Thus you can see the button is spread the rest of the area of the column it finds.
Now in the Behind I put a Messagebox to show the content of TextBox.
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(string.Format("Hi {0}", this.txtName.Text));
}Therefore you can see your name will be prompted to you.
If you have noticed the XAML minutely, you might think how can I define the property of Grid within other controls. Like as I defined Grid.Row=0 in each of the control. This is actually possible due to the use of Dependency properties. Its a new feature introduced with WPF. We will discuss in detail later on.
No comments:
Post a Comment