To create a WPF application
- On the File menu, click New Project.
- In the New Project dialog box, in the Templates pane, click WPF Application.
- In the Name box, type WPFWindow, and then click OK.
A new Windows Presentation Foundation project is created. A new window named Window1 appears. Its XAML markup is visible in the XAML editor of the Visual Basic integrated development environment (IDE) and appears as follows:<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> </Grid> </Window>
- Click Window1 to select it.
- In the XAML editor, change the Title attribute of the Window element from Window1 toWPF Application.
The XAML markup will now appear as follows:The text in the title bar of Window1 changes to WPF Application.<Window x:Class="Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WPF Application" Height="300" Width="300"> <Grid> </Grid> </Window>
Experiment by changing other attributes of the Window, such as Width and Height. When you are ready, continue with the next procedure.
Adding Controls to the WPF Window
In this procedure, you will add controls to your application. You will do this by clicking the control in the Toolbox, usually found on the left-hand side of the Visual Basic IDE, and then dragging the control onto your WPF window. You will set some properties for the control and then adjust the XAML markup to change the control's text and size.
To add a control to the WPF window
- From the Toolbox, drag a TextBox control to the upper right side of the WPF window.
- Select the TextBox control.
- In the Properties window, click the first arrow (Left) for the HorizontalAlignment property, as shown in the following illustration.
HorizontalAlignment property
This moves the TextBox from the right side of the WPF window to the left side.
- Set the following properties for the TextBox.
Property Value VerticalAlignment -- TOPTopWidth -- 7575Height --2626 - In the XAML editor, change the Width attribute of the TextBox element to 140, and change the Margin element to 30, 56, 0, 0, as shown in the following example.
The width and location of the TextBox change after you type the new values.<TextBox Height="26" HorizontalAlignment="Left" Margin="30,56,0,0" Name="TextBox1" VerticalAlignment="Top" Width="140" />
- Add a Button control to the WPF window, next to the TextBox.
- Change the text between the opening and closing Button tags from Button to Submit, as shown in the following example.
The text on the button changes after you enter the new value.<Button Height="23" HorizontalAlignment="Right" Margin="0,59,35,0" Name="Button1" VerticalAlignment="Top" Width="75">Submit</Button>
- Press F5 to run your program. A window containing the text box and button that you just added appears. Notice that you can click the button and type in the text box. You must add event handlers to the controls and then write code that instructs the computer what to do when the button is clicked.
0 comments:
Post a Comment