Pages

Wednesday, April 11, 2012

WPF Application


 FREE Resume posting for the Job Seekers in this site, (This site is JobSeekers Social networking of resumes of you and your Frineds ) you can send your resume from  all over the world  with experience and qualification dont send the projects and personal details please write the heading ex: 3+ years of expe in IT to this mail id softwarewalkins@gmail.com. it is like naukri and monster

It is open database of the resumes if you have any objections dont send your resume.  send the resumes from all over the world  Advantage of this site is you can copy the your Resume link in the address bar and paste in your compose mail so u need not to upload the resume when u r applying for any post

FREE Job Requirement posting on www.allwalkin.blogspot.com. Please forward to softwarewalkins@gmail.com

 Do you have any material in any Technology in IT please send the soft copy to this mail id softwarewalkins@gmail.com


Do you any 
 

IT Interview Questions Please send your questions to us our readers will give the answers

send your questions to softwarewalkins@gmail.com send the answers to same email id  
                
WPF Application
                     Commands in: Part I
Introduction

In this article we will see what Commands are and how we can use them in WPF.
Commands
It is often helpful to focus on what the user wants our application to do. WPF supports this through the command abstraction - a command is an action the application performs at the user's request.
WPF's command system lets us treat them as different expressions of the same command.

The command system lets a UI element provide a single handler for a command, reducing clutter and improving the clarity of your code.

The way in which a command is invoked isn't usually important. Whether the user presses Ctrl-C, selects the Edit -> Copy menu item, or clicks the Copy button on the toolbar, the application's response should be the same in each case: it should copy the current selection to the clipboard.

So let's have a sample application that will describe the above described things.
Creating a WPF Application

Fire up Visual Studio 2008 and Create a WPF Application, name it as CommandsWPF.
Now let's have few controls that would fulfill our sample to understand Commands.
Such as some Buttons displaying Copy, Cut, and Paste. And obviously a Text Box that would be the target where we can see the application working.
The following is the XAML for reference:
Code view of the above image
<Window x:Class="CommandsWPF.Window1"
02
 
03
04
    Title="Commands" Height="300" Width="388">
 
05
    <Grid>
06
        <Button x:Name="btnCopy" Height="23"
 
07
                Content="Copy" 
08
                Command="ApplicationCommands.Copy"
 
09
                CommandTarget="{Binding ElementName=txtData}"
10
                HorizontalAlignment="Left" Margin="31,25,0,0"
 
11
                VerticalAlignment="Top" Width="75"/>
12
        <Button x:Name="btnCut" Content="Cut" Height="23"
 
13
                Command="ApplicationCommands.Cut"
14
                CommandTarget="{Binding ElementName=txtData}"
 
15
                Margin="130,25,161,0" VerticalAlignment="Top" />
16
        <Button x:Name="btnPaste" Content="Paste" Height="23"
 
17
                Command="ApplicationCommands.Paste"
18
                CommandTarget="{Binding ElementName=txtData}"
 
19
                HorizontalAlignment="Right" Margin="0,25,55,0"
20
                VerticalAlignment="Top" Width="75"/>
 
21
        <TextBox x:Name="txtData" Margin="31,72,55,12" TextWrapping="Wrap"
22
                 VerticalScrollBarVisibility="Visible" />
 
23
    </Grid>
24
</Window>
Now we will see how Commands can be applied to the respective Clipboard Buttons.
Now run the application and see the output as, all the buttons are in disabled.
Even if you select the text nothing happens, that is because we haven't added the Target Control to handle.
Add the CommandTarget property and set the Bindings as follows:
Code view of the above image
01
<Button x:Name="btnCopy" Height="23"
02
        Content="Copy" 
 
03
        Command="ApplicationCommands.Copy"
04
        CommandTarget="{Binding ElementName=txtData}"
 
05
        HorizontalAlignment="Left" Margin="31,25,0,0"
06
        VerticalAlignment="Top" Width="75"/>
 
07
<Button x:Name="btnCut" Content="Cut" Height="23"
08
        Command="ApplicationCommands.Cut"
 
09
        CommandTarget="{Binding ElementName=txtData}"
10
        Margin="130,25,161,0" VerticalAlignment="Top" />
 
11
<Button x:Name="btnPaste" Content="Paste" Height="23"
12
        Command="ApplicationCommands.Paste"
 
13
        CommandTarget="{Binding ElementName=txtData}"
14
        HorizontalAlignment="Right" Margin="0,25,55,0"
 
15
        VerticalAlignment="Top" Width="75"/>
16
<TextBox x:Name="txtData" Margin="31,72,55,12" TextWrapping="Wrap"
 
17
         VerticalScrollBarVisibility="Visible" />
Now run the application and see how we have achieved the Clipboard Commands in Buttons.
As soon as you select the Text the Cut and Copy Buttons are enabled.
As soon as you perform the Copy or Cut Commands operation Paste Button is enabled.
By default TextBox has the Clipboard as ContextMenu, so even if we don't add commands in Buttons the Menu is displayed.
Hope this article helps
http://konac.kontera.com/javascript/lib/imgs/grey_loader.gif
for the beginning in Commands in WPF. In next part we will explore more.

You can also download the sample project used is the above example
Download

No comments:

Receive All Free Updates Via Facebook.