Custom Element Binding Window


   
  

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:MyNameSpace.CustomElementBinding" 
        Title="Custom Element Binding Demo">
    <StackPanel>
        <ScrollBar Orientation="Horizontal"
                   Margin="24" 
                   Maximum="100"
                   LargeChange="10"
                   SmallChange="1"  
                   Value="{Binding ElementName=simple, Path=Number,Mode=OneWayToSource}" />

    <src:SimpleElement x:Name="simple" HorizontalAlignment="Center" />
    </StackPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;

namespace MyNameSpace.CustomElementBinding
{
    class SimpleElement : FrameworkElement
    {
        public static DependencyProperty NumberProperty;
        static SimpleElement()
        {
            NumberProperty = DependencyProperty.Register("Number", typeof(double),typeof(SimpleElement),
                    new FrameworkPropertyMetadata(0.0,FrameworkPropertyMetadataOptions.AffectsRender));
        }
        public double Number
        {
            set { SetValue(NumberProperty, value); }
            get { return (double)GetValue(NumberProperty); }
        }
        protected override Size MeasureOverride(Size sizeAvailable)
        {
            return new Size(200, 250);
        }
        protected override void OnRender(DrawingContext dc)    
        {
            dc.DrawText(new FormattedText(Number.ToString(), 
                        CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                        new Typeface("Times New Roman"), 12, 
                        SystemColors.WindowTextBrush), 
                new Point(0, 0)); 
        }
    }
}

   
    
     


ShutdownMode.OnLastWindowClose


   
  


<Window x:Class="ApplicationShutdownSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" Height="300" Width="300" Loaded="MainWindow_Loaded">
  <StackPanel>
      <Label HorizontalAlignment="Left">Shutdown Mode:</Label>
      <ComboBox HorizontalAlignment="Left" Name="shutdownModeListBox"></ComboBox>
      <Button Click="explicitShutdownButton_Click">Shutdown Explicitly (Passing Exit Code)</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ApplicationShutdownSample
{
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.shutdownModeListBox.Items.Add("OnLastWindowClose");
            this.shutdownModeListBox.Items.Add("OnExplicitShutdown");
            this.shutdownModeListBox.Items.Add("OnMainWindowClose");
            this.shutdownModeListBox.SelectedValue = "OnLastWindowClose";
            this.shutdownModeListBox.SelectionChanged += new SelectionChangedEventHandler(shutdownModeListBox_SelectionChanged);
            Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
        }

        void shutdownModeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Application.Current.ShutdownMode = (ShutdownMode)Enum.Parse(typeof(ShutdownMode), this.shutdownModeListBox.SelectedValue.ToString());
        }


        void explicitShutdownButton_Click(object sender, RoutedEventArgs e)
        {
            int exitCode = 0;
            Application.Current.Shutdown(exitCode);
        }
    }
}

   
    
     


Window Loaded event


   
  


<Window x:Class="ApplicationShutdownSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Main Window" Height="300" Width="300" Loaded="MainWindow_Loaded">
  <StackPanel>
      <Label HorizontalAlignment="Left">Shutdown Mode:</Label>
      <ComboBox HorizontalAlignment="Left" Name="shutdownModeListBox"></ComboBox>
      <Button Click="explicitShutdownButton_Click">Shutdown Explicitly (Passing Exit Code)</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ApplicationShutdownSample
{
    public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.shutdownModeListBox.Items.Add("OnLastWindowClose");
            this.shutdownModeListBox.Items.Add("OnExplicitShutdown");
            this.shutdownModeListBox.Items.Add("OnMainWindowClose");
            this.shutdownModeListBox.SelectedValue = "OnLastWindowClose";
            this.shutdownModeListBox.SelectionChanged += new SelectionChangedEventHandler(shutdownModeListBox_SelectionChanged);
            Application.Current.ShutdownMode = ShutdownMode.OnLastWindowClose;
        }

        void shutdownModeListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Application.Current.ShutdownMode = (ShutdownMode)Enum.Parse(typeof(ShutdownMode), this.shutdownModeListBox.SelectedValue.ToString());
        }


        void explicitShutdownButton_Click(object sender, RoutedEventArgs e)
        {
            int exitCode = 0;
            Application.Current.Shutdown(exitCode);
        }
    }
}

   
    
     


Control the Size of UI Elements in a Form


   
      

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="300" Width="400">
    <StackPanel Margin="5" Orientation="Vertical">
        <Button Content="Button _A" Margin="2" />
        <Button Content="Button _B" HorizontalAlignment="Left" />
        <Button Content="Button _C" HorizontalAlignment="Center" />
        <Button Content="Button _D" HorizontalAlignment="Right" />
        <Button Content="Button _E" Height="40" Margin="2" />
        <Button Content="Button _F" Width="80" Margin="2" />
        <Button Content="Button _G" MinHeight="30" Margin="2" />
        <Button Content="Button _H" MinWidth="120" Margin="10,5,5,10" />
        <Button Content="Button _I" MaxWidth="200" Margin="2" />
    </StackPanel>
</Window>

   
    
    
    
    
    
     


Define the Tab Order of UI Elements in a Form


   
      

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="200" Width="300">
    <StackPanel>
        <Button Content="Button _A (1st tab)" TabIndex="0" Margin="2" />
        <Button Content="Button _B (4th tab)" TabIndex="3" Margin="2" />
        <Button Content="Button _C (2nd tab)" TabIndex="1" Margin="2" />
        <Button Content="Button _D (5th tab)" TabIndex="4" Margin="2" />
        <Button Content="Button _E (3rd tab)" TabIndex="2" Margin="2" />
        <Button Content="Button _F (6th tab)" TabIndex="5" Margin="2" />
    </StackPanel>
</Window>

   
    
    
    
    
    
     


About Dialog


   
      


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" SizeToContent="WidthAndHeight"
  Background="OrangeRed">
  <StackPanel>
  <Label FontWeight="Bold" FontSize="20" Foreground="White">
    WPF Version 
  </Label>
  <Label>2006</Label>
  <Label>Installed DLL:</Label>
  <ListBox>
    <ListBoxItem>1</ListBoxItem>
    <ListBoxItem>2</ListBoxItem>
  </ListBox>
  <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
    <Button MinWidth="75" Margin="10">Help</Button>
    <Button MinWidth="75" Margin="10">OK</Button>
  </StackPanel>
  <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>

   
    
    
    
    
    
     


About Dialog – Font Properties on Root


   
      


<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  Title="About WPF" SizeToContent="WidthAndHeight"
  FontSize="30" FontStyle="Italic"
  Background="OrangeRed">
  <StackPanel>
    <Label FontWeight="Bold" FontSize="20" Foreground="White">
      WPF Version
    </Label>
    <Label>2010</Label>
    <Label>Installed Dll:</Label>
    <ListBox>
      <ListBoxItem>1</ListBoxItem>
      <ListBoxItem>2</ListBoxItem>
    </ListBox>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10">Help</Button>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>You have successfully registered this product.</StatusBar>
  </StackPanel>
</Window>