Sunday, January 15, 2012

WPF Overview (XAML)

This topic describes the features of the XAML language and demonstrates how you can use XAML to write Windows Presentation Foundation (WPF) with Example.

What is XAML in WPF?
XAML is Extensible Application Markup Language. As applied to the .NET Framework programming model, XAML simplifies creating a UI for a .NET Framework application. You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files, joined to the markup through partial class definitions. XAML directly represents the instantiation of objects in a specific set of backing types defined in assemblies. This is unlike most other markup languages, which are typically an interpreted language without such a direct tie to a backing type system. XAML enables a workflow where separate parties can work on the UI and the logic of an application, using potentially different tools.
When represented as text, XAML files are XML files that generally have the .xaml extension. The files can be encoded by any XML encoding, but encoding as UTF-8 is typical.

New in XAML of .NET 4.0 :
1.    Easy Object References with {x:Reference}
2.    Built-in Types
3.    Generics in XAML with {x:TypeArguments}
4.    Support for Arbitrary Dictionary Keys  
5.    Use of Non-Default Constructors with {x:Arguments}
6.    Use of Static Factory Methods with{ x:FactoryMethod}

1: Easy Object References with {x:Reference}
<Label Target="{Binding ElementName=firstName}">FirstName</Label>
<TextBox x:Name="firstName" />

<Label Target="{x:Reference firstName}">FirstName</Label>
<TextBox x:Name="firstName" />

2: Built-in Types
    <x:Int16><x:Int16/>
    <x:Int32><x:Int32/>
    <x:Int64><x:Int64/>
    <x:String> </x:String>
    <x:Object ><x:Object/>
    <x:Boolean><x:Boolean/>
    <x:Char><x:Char/>
    <x:String><x:String/>
    <x:Decimal><x:Decimal/>
    <x:Single><x:Single/>       
    <x:Double><x:Double/>
    <x:TimeSpan><x:TimeSpan/>
    <x:Uri><x:Uri/>
    <x:Byte><x:Byte/>
    <x:Array><x:Array/>
    <x:List><x:List/>
    <x:Dictionary> <x:Dictionary/>

3: Generics in XAML with {x:TypeArguments}

<ObservableCollection x:TypeArguments="Employee">
    <l:Employee FirstName="John" Name="Doe" />
    <l:Employee FirstName="Tim" Name="Smith" />
</ObservableCollection />
 
4: Support for Arbitrary Dictionary Keys:

<StreamGeometry>M 0 0 L 12 8 l 9 12 z
    <x:Key><x:Double>10.0</x:Double></x:Key>
</StreamGeometry>
 
5: Use of Non-Default Constructors with {x:Arguments}

<DateTime>
    <x:Arguments>
        <x:Int64>1000</x:Int64>
    </x:Arguments>
</DateTime>
 
6: Use of Static Factory Methods with{ x:FactoryMethod}

               <Guid x:FactoryMethod="Guid.NewGuid" />
 
 
Controls in WPF
            
1.ItemsControl
2.Calendar
3.ToolTips
4.Expander
5.TextBox
6.DataGrid
7.LivePreview ComboBox
8.ContextMenu
9.Menus
10.PasswordBox
11.Dialogs
12.Slider
13.RadioButton
14.Popup
15.ListBox
16.ListView
17.TextBlock


1: ItemsControl
     <ListBox l:ItemsControlHelper.ScrollToLastItem="true" />
 
2: Calendar
<Calendar SelectionMode="{Binding SelectedItem,ElementName=selectionmode}">
    <Calendar.BlackoutDates>
        <CalendarDateRange Start="01/01/2010" End="01/06/2010" />
        <CalendarDateRange Start="05/01/2010" End="05/03/2010" />
    </Calendar.BlackoutDates>
</Calendar>
 
3: ToolTips
<Button Content="Submit">
    <Button.ToolTip>
        <ToolTip>
            <StackPanel>
                <TextBlock FontWeight="Bold">Submit!!</TextBlock>
                <TextBlock>Submits the request to the server!</TextBlock>
            </StackPanel>
        </ToolTip>
    </Button.ToolTip>
</Button>
 
4: Expander
<Expander Header="More Options">
    <StackPanel Margin="5,4,0,0">
        <CheckBox Margin="3" Content="Option 1" />
        <CheckBox Margin="3" Content="Option 2" />
        <CheckBox Margin="3" Content="Option 3" />
    </StackPanel>
</Expander>
 
5: TextBox
        <TextBox SpellCheck.IsEnabled="True" Language="Hindi" />

6: DataGrid
        <DataGrid ItemsSource="{Binding Customers}" 
          SelectionMode="Extended" SelectionUnit="Cell" />

7: LivePreview ComboBox
    <StackPanel>
        <TextBlock Text="Preview Value:" />
        <TextBlock Text="{Binding LivePreviewItem, ElementName=liveBox}" />
        <l:LivePreviewComboBox x:Name="liveBox"/>
    </StackPanel>
 
8: ContextMenu
<RichTextBox>
    <RichTextBox.ContextMenu>
        <ContextMenu>
            <MenuItem Command="Cut">
                <MenuItem.Icon>
                    <Image Source="Images/cut.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="Copy">
                <MenuItem.Icon>
                    <Image Source="Images/copy.png" />
                </MenuItem.Icon>
            </MenuItem>
            <MenuItem Command="Paste">
                <MenuItem.Icon>
                    <Image Source="Images/paste.png" />
                </MenuItem.Icon>
            </MenuItem>
        </ContextMenu>
    </RichTextBox.ContextMenu>
</RichTextBox>
 
9: Menus        
<Menu IsMainMenu="True">
    <MenuItem Header="File" />
    <MenuItem Header="Edit" />
    <MenuItem Header="View" />
    <MenuItem Header="Window" />
    <MenuItem Header="Help" />
</Menu>
 
10: PasswordBox
<StackPanel>
      <Label Content="Password:" />
      <PasswordBox x:Name="passwordBox" Width="130" />
</StackPanel>
 
11: Dialogs
<StackPanel>
        <Button Content="Cancel" IsCancel="True" />
        <Button Click="OkClick" Content="Ok!!" IsDefault="true" />
</StackPanel>
 
12: Slider
<Slider Minimum="0 Maximum="20" IsSnapToTickEnabled="True"
        TickFrequency="2">
</Slider>
 
13: RadioButton
<StackPanel>
    <RadioButton GroupName="Os" Content="Windows XP" IsChecked="True"/>
    <RadioButton GroupName="Os" Content="Windows Vista" />
    <RadioButton GroupName="Os" Content="Windows 7" />
    <RadioButton GroupName="Office" Content="Microsoft Office 2003"/>
    <RadioButton GroupName="Office" Content="Open Office"/>
</StackPanel>
 
14: Popup
<Popup StaysOpen="False" />
 
15: ListBox
<ListBox Margin="40">
    <ListBoxItem>Delhi</ListBoxItem>
    <ListBoxItem>U.P.</ListBoxItem>
    <ListBoxItem>Hariyana</ListBoxItem>
    <ListBoxItem>Panjab</ListBoxItem>
</ListBox>

16: ListView
 
<ListView>
    <ListView.View>
        <GridView>
            <GridView.ColumnHeaderContainerStyle>
               <Style>
                   <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
               </Style>
            </GridView.ColumnHeaderContainerStyle>
            <GridView.Columns>
                ...
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>
 
 
17: TextBlock
        <TextBlock Text="This is a multiline text!!" 
           LineHeight="25" LineStackingStrategy="BlockLineHeight" />

Data Binding control

1
1.Debug DataBinding Issues
2.Data Viewing, Sorting and Filtering  
3.Data Binding Overview
4.Popular Data Binding Expressions
5.How to Bind EnumValues
6.Value Converters
7.Data Validation
8.Elegant way for INotifyPropertyChanged

Visit on more details INotifyPropertyChanged:


 Layout 
                                        
1.Introduction to WPF Layout
2.Grid Panel
3.Canvas Panel
4.StackPanel
5.Dock Panel
6.ViewBox
7.Wrap Panel

1: Introduction to WPF Layout
2: Grid Panel
3: Canvas Panel
4: StackPanel
5: Dock Panel
6: ViewBox   
      7: Wrap Panel 

Input
  
1.Keyboard
2.Mutli Touch
3.Mouse