Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Windows Presentation Foundation (WPF)


by Sujith Chandana


In previous article we discussed brief introduction about WPF. Now let’s move to develop WPF application.

Before start programming in WPF I assume that you are familiar applications with .NET Framework using managed technologies like ASP.NET and Windows Forms. And you should have fundamentals of instantiate classes, change properties, handle methods and handle events, with your favorite programming languages in .NET Framework, such as C# or VB. 


System requirements

    Visual studio 2005 with .NET framework 3.0 or later version

      In addition to that the windows software development kit (SDK), specifically the .net framework tools includes is recommended.


Controls in WPF

Controls are the important things with arranging applications. So far, you have seen how to create instances of controls, set properties, and handle events through windows applications.

The following picture shows how a control work with a program. 

 


Figure1: Control vs. a Program

You can create a new WPF project like other applications in visual studio. After creating new project you can see tool box with controls and property window to change properties which you use in your project.


Figure 2: Create New WPF Application

   

                                                                                                                                          Figure 3: Tool Box           

   

  

Figure 4: Property Window

Buttons

Clicking of the button controls are up to the application developers. Clicking on a Check Box or Radio Button expresses a choice, and does not normally have any immediate effect beyond visually reflecting that choice.

Markup for a button

<Button Height="23" HorizontalAlignment="Left" Margin="58,49,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click">Button</Button>

   

An XML attribute specifies the handler for the Click event. This indicates that the code behind for the XAML must contain a method with the name specified in the markup. Ex:

private void button1_Click(object sender, RoutedEventArgs e)

    {

    MessageBox.Show("Button was Clicked");

    }

WPF supports this style of keyboard access with the Access Text element. You can wrap this around some text, putting an underscore in front of the letter that will act as the access key. You can see by pressing Alt key.


WPF supports this style of keyboard access with the Access Text element. You can wrap this around some text, putting an underscore in front of the letter that will act as the access key. You can see by pressing Alt key.

<Button Height="23" ....... >

    <AccessText>_Button</AccessText>    

    </Button>

                                                                                                                                                                                                                                                                                                                                                                                                                                                         
      




Figure 5: Button
with  Access key

If you want use access key without access text do this way

<Button Width="75">_Button</Button>

Radio button with different group

<StackPanel>

    <RadioButton GroupName="Fuel">Petrol</RadioButton>

    <RadioButton GroupName="Fuel">Diesel</RadioButton>

   

    <RadioButton GroupName="Induction">Unforced</RadioButton>

    <RadioButton GroupName="Induction">Mechanicalsupercharger</RadioButton>

    <RadioButton GroupName="Induction">Turbocharger</RadioButton>

 </StackPanel>

If you want to add another things to control radio buttons press Ctrl+Space and get following menu and try to go through it.

 

Figure 6: Aditional properties in Radio Button

Tool Tip

A box with text that appears like below when you hover over the mouse an associated control and disappears when you move the mouse away.

<Button  Name="button1" Click="button1_Click"  ...

 ToolTip="Click here to submit your request" ></Button>     

                                                                                                                                                                                       Figure 7: Tool Tip  

WPF Tool Tip can hold anything because, flexibility of the WPF’s content controls. Look at following example it shows how can construct Microsoft office 2007 style Screen Tip.

<CheckBox Margin="26,32.347,118,0" Height="54.49" VerticalAlignment="Top">

    CheckBox

   <CheckBox.ToolTip>

<StackPanel>

    <Label FontWeight="Bold" Background="Blue" Foreground="White">

   The CheckBox

    </Label>

  

<CheckBox Margin="26,32.347,118,0" Height="54.49" VerticalAlignment="Top">

    CheckBox

   <CheckBox.ToolTip>

<StackPanel>

    <Label FontWeight="Bold" Background="Blue" Foreground="White">

   The CheckBox

    </Label>

    <TextBlock Padding="10" TextWrapping="WrapWithOverflow" Width="200">

   CheckBox is a familiar control. But in WPF, it’s not much more     than a ToggleButton styled differently!

</TextBlock>

<Line Stroke="Black" StrokeThickness="1" X2="200"/>

  <StackPanel Orientation="Horizontal">

<Label FontWeight="Bold">Press F1 for more help.</Label>

  </StackPanel>

   </StackPanel>

    </CheckBox.ToolTip>

 </CheckBox>

   

Will show following figure


Figure 8: Tool TIP

You can also add picture to the Tool Tip using like below.

<Image Margin=”2” Source=”help.gif”/>

Show Duration control how long the ToolTip should be displayed.

     ToolTipService.ShowDuration=”3000

You can get a Tool Tip to appear when hovering over a disable element by using

<Button ToolTipService.ShowOnDisabled=”True”>. . .  . . .</Button>  in XAML or

ToolTipService.SetShowOnDisabled(myButton, true); in C#.

So, in later articles will discuss about rest of the controls in WPF.

Share/Save
No votes yet

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options