Search This Blog

Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Wednesday, January 16, 2013

WPF CheckBox Control (C#)

CheckBox enables the user to select whether an option is on or off.

XAML:


<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="10,10,0,0" Name="checkBox1" VerticalAlignment="Top" Checked="checkBox1_Checked" Unchecked="checkBox1_Unchecked" />
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="12,32,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

C# code behind:


 private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
                MessageBox.Show("checkBox1 Checked");
        }
        private void checkBox1_Unchecked(object sender, RoutedEventArgs e)
        {
              MessageBox.Show("checkBox1 UnChecked");
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked == true)
                MessageBox.Show("checkBox1 Checked");
            else
                MessageBox.Show("checkBox1 UnChecked");
        }

Runtime:


WPF Btutton (C#)

This control is designed to be clicked to enable the user to make a choice, to close a dialog box, or to perform another action.

XAML

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>

// c# code behind 

private void button1_Click(object sender, RoutedEventArgs e)
{
   MessageBox.Show("Button1 Click"); 
}


Runtime:




Home Next: CheckBox Control

Introduction to Microsoft Windows Applications with WPF (C#)

The user interface (UI) is the visual representation of your application.






A well UI design that flows logically can provide a consistent user experience from application to application and make learning new applications easy for users.

Windows Forms historically has been the basis for most Microsoft Windows applications and can be configured to provide a variety of user interface  options.






WPF is the successor to Windows Forms for desktop application development. WPF applications differ from traditional Windows Forms applications in several ways, the most important is that the code for the user interface is separate from the code for application functionality.
 
Windows Presentation Foundation (WPF) is a next-generation presentation system for building Windows client applications with visually stunning user experiences.

The core of WPF is a resolution-independent and vector-based rendering engine that is built to take advantage of modern graphics hardware. WPF extends the core with a comprehensive set of application-development features that include Extensible Application Markup Language (XAML), controls, data binding, layout3-D graphics, animation,  media, text, etc.

WPF individual controls





These controls have a single purpose in an application; buttons are clicked, text boxes receive and display text, etc.
WPF items controls
Are designed to contain groups of related items: Menu, ListBox, TreeView.
WPF Layout controls
 
Contain multiple nested controls of any type and provide built-in logic for the visual layout of those controls: Grid, StackPanel, Canvas
 

WPF Content Controls

A ontent control derives from the  ContentControl class and can contain a single nested element.
This nested element can be of any type and can be set or retrieved in code through theContent
property.

 


Create Microsoft Windows Applications with WPF (C#)

 
Start Microsoft Visual Studio and from Menu->File select WPF Application


 













For an empty application the XAML contains:
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
    </Grid>
</Window>
 
Grid defines a flexible grid area that consists of columns and rows.
 
Label and TextBox control

Label It is  container for content
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Name="label1">Label control</Label>
    </Grid>
</Window>

Press F5, result is:


 

Labels containsupport for mnemonic keys  when the Alt key is pressed with the mnemonic key.














The mnemonic key is specified by preceding the desired key with the underscore (_) symbol
 

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Content="_Code" Target="{Binding ElementName=TextBox1}"></Label>
        <TextBox Name="TextBox1" Margin="65,1,94,287"> </TextBox>
    </Grid>
</Window>

 

 

Next: ButtonControl