Expander control

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Control Review" Height="211" Width="421" WindowStartupLocation="CenterScreen">
    <Grid>
        <Button Name="btnPurchaseOptions" Height="100" Width = "300">
            <StackPanel>
                <Label Name="lblInstructions" Foreground = "DarkGreen" Content = "Select Your Options and Press to Commit"/>
                <StackPanel Orientation = "Horizontal">
                    <Expander Name="colorExpander" Header = "Color">
                        <Label Foreground = "DarkGreen" Content = "AAA"/>
                    </Expander>
                    <Expander Name="MakeExpander" Header = "Make">
                        <Label Foreground = "DarkGreen" Content = "AAA"/>
                    </Expander>
                    <Expander Name="paymentExpander" Header = "Payment Plan">
                        <Label Foreground = "DarkGreen" Content = "AAA"/>
                    </Expander>
                </StackPanel>
            </StackPanel>
        </Button>
    </Grid>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WpfApplication1
{
  public partial class MainWindow : System.Windows.Window
  {
    public MainWindow()
    {
      InitializeComponent();
      lblInstructions.FontSize = 14;
      btnPurchaseOptions.Click +=new RoutedEventHandler(btnPurchaseOptions_Click);
    }
    private void btnPurchaseOptions_Click(object sender, RoutedEventArgs e)
    {
      MessageBox.Show("Button has been clicked");
    }
  } 
}

   
    
     


Use the Expander control and set the ExpandDirection property

image_pdfimage_print


   
  

<Page x:Class="ExpanderDirectionExample.Page1"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Grid>
    
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="200"/>
      <ColumnDefinition Width="150"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
      <RowDefinition/>
    </Grid.RowDefinitions>
    
    <StackPanel Grid.Column="0" Grid.Row="0">
      <Expander Name="myExpander" Background="Tan" 
                HorizontalAlignment="Left" Header="My Expander" 
                ExpandDirection="Down" IsExpanded="True" Width="100">
        <TextBlock TextWrapping="Wrap">
          this is a test
        </TextBlock>
      </Expander>
    </StackPanel>
    
    <StackPanel Grid.Column="1" Grid.Row="0">
      <TextBlock Margin="0, 10, 3, 3" FontSize="12" TextWrapping="Wrap">
        Click to change the ExpandDirection property on My Expander
      </TextBlock>
      <StackPanel>
        <RadioButton Name="ExpandDown" Margin="0,10,0,10" 
                  IsChecked="True"
                  Checked="ChangeExpandDirection"
                  GroupName="ExpandDirectionProperty">
           Expand Down
        </RadioButton>
        <RadioButton Name="ExpandUp" Margin="0,0,0,10"
                  Checked="ChangeExpandDirection"
                  GroupName="ExpandDirectionProperty">
           Expand Up
        </RadioButton>
        <RadioButton Name="ExpandLeft" Margin="0,0,0,10"
                  Checked="ChangeExpandDirection"
                  GroupName="ExpandDirectionProperty">
          Expand Left
        </RadioButton>
        <RadioButton Name="ExpandRight" Margin="0,0,0,10"
                  Checked="ChangeExpandDirection"
                  GroupName="ExpandDirectionProperty">
          Expand Right
        </RadioButton>
      </StackPanel>
    </StackPanel>
  </Grid>
</Page>


//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.Navigation;
using System.Windows.Shapes;

namespace ExpanderDirectionExample
{
    public partial class Page1 : Page
    {
        private void ChangeExpandDirection(object sender, RoutedEventArgs e)
        {
            if ((Boolean)ExpandDown.IsChecked)
                myExpander.ExpandDirection = ExpandDirection.Down;
            else if ((Boolean)ExpandUp.IsChecked)
                myExpander.ExpandDirection = ExpandDirection.Up;
            else if ((Boolean)ExpandLeft.IsChecked)
                myExpander.ExpandDirection = ExpandDirection.Left;
            else if ((Boolean)ExpandRight.IsChecked)
                myExpander.ExpandDirection = ExpandDirection.Right;

            myExpander.IsExpanded = true;
        }
    }
}

   
    
     


FlowDocument with Section

image_pdfimage_print


   
     

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Section LineHeight="2" Foreground="White" Background="Black">
    <Paragraph FontSize="18">WPF Unleashed</Paragraph>
    <Paragraph FontSize="30" FontWeight="Bold">Notes from Chapter 1</Paragraph>
  </Section>
</FlowDocument>

   
    
    
    
    
     


Ellipse MouseMove event

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

           myEllipse.MouseMove += myEllipse_MouseMove;

        }
        void myEllipse_MouseMove(object sender, MouseEventArgs e)
        {
            Debug.WriteLine(Mouse.GetPosition(myEllipse));
        }


    }
}

   
    
     


Ellipse Mouse up event

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MouseInput" Height="300" Width="300">
    <Grid>
      <Ellipse Fill="Blue" x:Name="myEllipse" />
    </Grid>
</Window>


//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;


namespace WpfApplication1
{
    public partial class Window1 : System.Windows.Window
    {
        public Window1()
        {
            InitializeComponent();

           myEllipse.MouseUp += myEllipse_MouseUp;

        }

        void myEllipse_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(null);
        }


    }
}

   
    
     


Cast event sender to a control

image_pdfimage_print


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="MyNameSpace.IncludeApplicationDefinition.MyWindow"
        Title="Include Application Definition" 
        SizeToContent="WidthAndHeight" 
        ResizeMode="CanMinimize">
    <Button HorizontalAlignment="Center" VerticalAlignment="Center" Margin="1.5in" Click="ButtonOnClick">
        Click
    </Button>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace MyNameSpace.IncludeApplicationDefinition
{
    public partial class MyWindow : Window
    {
        public MyWindow()
        {
            InitializeComponent();
        }
        void ButtonOnClick(object sender, RoutedEventArgs args)
        {
            Button btn = sender as Button;
            MessageBox.Show(btn.Content + "&#039; has been clicked.");
        }
    }
}

   
    
     


Press F1 to get help

image_pdfimage_print


   
  
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Press F1 to get help" Height="250" Width="400">
  <DockPanel>
    <Menu DockPanel.Dock ="Top" HorizontalAlignment="Left" Background="White">
      <MenuItem Header="_Edit">
        <MenuItem Command ="Copy"/>
        <MenuItem Command ="Cut"/>
        <MenuItem Command ="Paste"/>
      </MenuItem>
    </Menu>
    <TextBox AcceptsReturn ="True" Foreground ="Black" Background ="AliceBlue"></TextBox>
  </DockPanel>   
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApplication1
{
  public partial class MainWindow : System.Windows.Window
  {
    public MainWindow()
    {
      InitializeComponent();

      CommandBinding helpBinding = new CommandBinding(ApplicationCommands.Help);
      helpBinding.CanExecute += CanHelpExecute;
      helpBinding.Executed += HelpExecuted;
      CommandBindings.Add(helpBinding);
    }

    private void CanHelpExecute(object sender, CanExecuteRoutedEventArgs e)
    {
      e.CanExecute = true;
    }
    private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
    {
      MessageBox.Show("support", "Help!");
    }
  }
}