Throw Exception from button click action

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="AppEventsSample" Height="300" Width="300">
    <Grid>
    <Button Height="23" x:Name="shutdownButton" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75">Shutdown</Button>
    <Label Height="25" HorizontalAlignment="Left" Margin="10" x:Name="label1" VerticalAlignment="Top" Width="100">Application Events:</Label>
    <ListBox Margin="20" x:Name="eventsListBox" ItemsSource="{Binding}" />
    <Button Height="23" Margin="10" Name="exceptionButton" VerticalAlignment="Bottom">Exception</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 Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();
    //  this.DataContext = ((App)Application.Current).EventLog;
    }

    protected override void OnInitialized(EventArgs e) {
      base.OnInitialized(e);
      exceptionButton.Click += new RoutedEventHandler(exceptionButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
    }

    void exceptionButton_Click(object sender, RoutedEventArgs e) {
      throw new Exception("Opps! Unhandled exception!");
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown(111);
    }

  }
}

   
    
     


Catch XamlParseException

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="FlowDocReader Load/Save Sample" Width="640" Height="480">
  <StackPanel>
    <FlowDocumentReader Name="FlowDocRdr" Grid.Row="1"/>
  </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;
using System.Windows.Forms;
using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();

            FlowDocument content = null;
            
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";

            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FileStream xamlFile = openFile.OpenFile() as FileStream;
                if (xamlFile == null) return;
                else
                {   
                    try 
                    { 
                        content = XamlReader.Load(xamlFile) as FlowDocument; 
                        if (content == null) 
                            throw(new XamlParseException("The specified file could not be loaded as a FlowDocument."));
                    }
                    catch (XamlParseException e)
                    {
                        String error = "There was a problem parsing the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }
                    catch (Exception e) 
                    {
                        String error = "There was a problem loading the specified file:

";
                        error += openFile.FileName;
                        error += "

Exception details:

";
                        error += e.Message;
                        System.Windows.MessageBox.Show(error);
                        return;
                    }

                    FlowDocRdr.Document = content;
                }
            }
        }
    }
}

   
    
     


Manage unhandled exceptions that are thrown on secondary user interface (UI) threads.

image_pdfimage_print
   
  


<Application
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.App"
  StartupUri="MainWindow.xaml" 
  DispatcherUnhandledException="App_DispatcherUnhandledException" />

//File:Window.xaml.cs
using System; 
using System.Text; 
using System.Windows;
using System.Windows.Threading; 

namespace WpfApplication1
{
    public partial class App : Application
    {
        void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0}
", e.Exception.InnerException.Message);
            sb.AppendFormat("{0}
", e.Exception.Message);
            sb.AppendFormat("Exception handled on main UI thread {0}.", e.Dispatcher.Thread.ManagedThreadId);
            MessageBox.Show(sb.ToString());

            e.Handled = true;
        }
    }
}

   
    
     


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));
        }


    }
}