Hosting a Windows Forms Property Grid in WPF


   
  

<Window x:Class="WpfApplication1.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:swf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
  Title="Hosting a Windows Forms Property Grid in WPF"
  Loaded="Window_Loaded">
    <Grid>
      <WindowsFormsHost>
        <swf:PropertyGrid x:Name="propertyGrid"/>
      </WindowsFormsHost>
    </Grid>
</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.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            propertyGrid.SelectedObject = this;
        }
    }
}

   
    
     


Launch a window with defined 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="Simple XAMl Viewer" Height="338" Width="1041" 
    Loaded="Window_Loaded" Closed="Window_Closed" WindowStartupLocation="CenterScreen" >
  <DockPanel  LastChildFill="True" >
    <Button DockPanel.Dock="Top"  Name = "btnViewXaml" Width="100" Height="40"
              Content ="View Xaml" Click="btnViewXaml_Click" />
    <TextBox AcceptsReturn ="True" Name ="txtXamlData"
       BorderBrush ="Blue" VerticalScrollBarVisibility="Auto" AcceptsTab="True" TextDecorations="None">
    </TextBox>
  </DockPanel>
</Window>
//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

using System.IO;
using System.Windows.Markup;

namespace WpfApplication1
{
  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
    }

    private void btnViewXaml_Click(object sender, RoutedEventArgs e)
    {
      File.WriteAllText("YourXaml.xaml", txtXamlData.Text);
      Window myWindow = null;
      try
      {
        using (Stream sr = File.Open("YourXaml.xaml", FileMode.Open))
        {
          myWindow = (Window)XamlReader.Load(sr);
          myWindow.ShowDialog();
        }
      }
      catch (Exception ex)
      { MessageBox.Show(ex.Message); }
    }
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        txtXamlData.Text =
        "<Window xmlns="http://schemas.microsoft.com"
        + "/winfx/2006/xaml/presentation"
"
        + "xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml""
        + " Height ="400" Width ="500" WindowStartupLocation="CenterScreen">
"
        + "<StackPanel>
"
        + "</StackPanel>
"
        + "</Window>";
    }
    private void Window_Closed(object sender, EventArgs e)
    {
      File.WriteAllText("YourXaml.xaml", txtXamlData.Text);
    }
  }
}

   
    
     


Window Ownership


   
  
<Window x:Class="Windows.WindowOwnership"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Owner" Height="300" Width="300"
    >
  <Button Margin="3" Click="cmdCreate_Click">Create Owned Window</Button>
</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 Windows
{

    public partial class WindowOwnership : System.Windows.Window
    {

        public WindowOwnership()
        {
            InitializeComponent();
        }

        private void cmdCreate_Click(object sender, RoutedEventArgs e)
        {
            WindowOwnership win = new WindowOwnership();
            win.Owner = this;
            win.Title = "Owned Window";
            win.Height = this.Height/2;
            win.Width = this.Width/2;
            win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            win.Show();
        }
    }
}

   
    
     


Window mouse up event


   
  

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800"
    MouseUp="Window_MouseUp">
  
    <Grid Name="contentGrid" MouseDown="Generic_MouseDown" PreviewMouseDown="Generic_MouseDown" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


Window Preview mouse down event


   
  


<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800"
    PreviewMouseDown="Generic_MouseDown" MouseUp="Window_MouseUp">
  
    <Grid Name="contentGrid" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


Window mouse down event


   
  


<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.Window1"
    Title="Routed Events" Height="400" Width="800"
    MouseDown="Generic_MouseDown" PreviewMouseDown="Generic_MouseDown" MouseUp="Window_MouseUp">
  
    <Grid Name="contentGrid" Background="Red">
        <Rectangle Name="clickMeRectangle" 
                   Height="70" 
                   Width="70" 
                   Stroke="Black" 
                   Fill="CadetBlue" />
        <Button Name="clickMeButton" 
                Height="23" 
                HorizontalAlignment="Right" 
                VerticalAlignment="Top" 
                Width="70" 
                Click="clickMeButton_Click">Click Me</Button>
        <TextBlock Name="outputText" />
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
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.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        private void Generic_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Console.WriteLine(outputText.Text);
            Console.WriteLine(e.RoutedEvent.Name);
            Console.WriteLine(sender.ToString());
            Console.WriteLine(((FrameworkElement)e.Source).Name);
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            outputText.Text = outputText.Text;
        }

        private void clickMeButton_Click(object sender, RoutedEventArgs e)
        {
            outputText.Text = "Button clicked:" + outputText.Text;
        }
    }
}

   
    
     


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>