Change window cursor

image_pdfimage_print


   
  

<Window x:Class="EightBall.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Eight Ball Answer" Height="328" Width="412" >
  <Grid>
    <Grid.RowDefinitions>
      <RowDefinition Height="*" />
      <RowDefinition Height="Auto" />
      <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.Background>
      <LinearGradientBrush>
        <LinearGradientBrush.GradientStops>
          <GradientStop Offset="0.00"  Color="Red" />
          <GradientStop Offset="0.50" Color="Indigo" />
          <GradientStop Offset="1.00" Color="Violet" />
        </LinearGradientBrush.GradientStops>
      </LinearGradientBrush>
    </Grid.Background>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtQuestion" 
             TextWrapping="Wrap" FontFamily="Verdana" FontSize="24"
             Grid.Row="0" >
      text
    </TextBox>
    <Button VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,0,0,20" Width="127" Height="23" Name="cmdAnswer"
            Click="cmdAnswer_Click" 
            Grid.Row="1">      
      Click
      </Button>
    <TextBox VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,10,13,10" Name="txtAnswer" 
             TextWrapping="Wrap" IsReadOnly="True" FontFamily="Verdana" FontSize="24" Foreground="Green"
             Grid.Row="2">
      text
    </TextBox>    
  </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.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Input;

namespace EightBall
{
    public partial class Window1 : Window
    {

        public Window1()
        {
            InitializeComponent();
        }

        private void cmdAnswer_Click(object sender, RoutedEventArgs e)
        {           
            this.Cursor = Cursors.Wait;
            System.Threading.Thread.Sleep(TimeSpan.FromSeconds(1));

            txtAnswer.Text = "asdf";
            this.Cursor = null;
        }

    }
}

   
    
     


Launch a window with defined XAML

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

   
    
     


Hosting a Windows Forms Property Grid in WPF

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

   
    
     


Window.DragMove

image_pdfimage_print


   
  


<Window x:Class="GadgetWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Height="300" Width="300"
  AllowsTransparency="True" WindowStyle="None" Background="Transparent"
  MouseLeftButtonDown="Window_MouseLeftButtonDown">
  <Grid>
    <Ellipse Fill="Red" Opacity="0.5" Margin="20">
      <Ellipse.BitmapEffect>
        <DropShadowBitmapEffect/>
      </Ellipse.BitmapEffect>
    </Ellipse>
    <Button Margin="100" Click="Button_Click">Close</Button>
  </Grid>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Input;

public partial class GadgetWindow : Window
{
  public GadgetWindow()
  {
    InitializeComponent();
  }
  void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  {
    this.DragMove();
  }
  void Button_Click(object sender, RoutedEventArgs e)
  {
      this.Close();
  }
}

   
    
     


Window.CommandBindings

image_pdfimage_print


   
  
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        x:Class="AboutDialog"
        Title="About WPF Unleashed" SizeToContent="WidthAndHeight"
        Background="OrangeRed">
  <Window.CommandBindings>
    <CommandBinding Command="Help" CanExecute="HelpCanExecute" Executed="HelpExecuted"/>
  </Window.CommandBindings>
  <StackPanel>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
      <Button MinWidth="75" Margin="10" Command="Help" Content="{Binding RelativeSource={RelativeSource Self}, Path=Command.Text}"/>
      <Button MinWidth="75" Margin="10">OK</Button>
    </StackPanel>
    <StatusBar>asdf</StatusBar>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System.Windows;
using System.Windows.Input;

public partial class AboutDialog : Window
{
    public AboutDialog()
    {
        InitializeComponent();
    }

    void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
    {
        e.CanExecute = true;
    }
    
    void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        System.Diagnostics.Process.Start("http://www.kutayzorlu.com/java2s/com");
    }
}

   
    
     


Create a non-rectangular window

image_pdfimage_print


   
  

<Window x:Class="NonRectangularWindowSample.NonRectangularWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="NonRectangularWindowSample" SizeToContent="WidthAndHeight" 
  MouseLeftButtonDown="NonRectangularWindow_MouseLeftButtonDown"
  WindowStyle="None" 
  AllowsTransparency="True" 
  Background="Transparent">
  <Canvas Width="200" Height="200" >
    <Path Stroke="DarkGray" StrokeThickness="2">

      <Path.Fill>
        <LinearGradientBrush StartPoint="0.2,0" EndPoint="0.8,1" >
          <GradientStop Color="White"  Offset="0"></GradientStop>
          <GradientStop Color="Red"  Offset="0.45"></GradientStop>
          <GradientStop Color="LightBlue" Offset="0.9"></GradientStop>
          <GradientStop Color="Gray" Offset="1"></GradientStop>
        </LinearGradientBrush>
      </Path.Fill>

      <Path.Data>
        <PathGeometry>
          <PathFigure StartPoint="40,20" IsClosed="True">
            <LineSegment Point="160,20"></LineSegment>
            <ArcSegment Point="180,40" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
            <LineSegment Point="180,80"></LineSegment>
            <ArcSegment Point="160,100" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
            <LineSegment Point="90,100"></LineSegment>
            <ArcSegment Point="20,80" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
            <LineSegment Point="20,40"></LineSegment>
            <ArcSegment Point="40,20" Size="20,20" SweepDirection="Clockwise"></ArcSegment>
          </PathFigure>
        </PathGeometry>
      </Path.Data>

    </Path>

    <Label Width="200" Height="120" FontSize="15" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">Drag Me</Label>
    <Button Canvas.Left="155" Canvas.Top="30" Click="closeButtonRectangle_Click">
      <Button.Template>
        <ControlTemplate>
          <Canvas>
            <Rectangle Width="15" Height="15" Stroke="Black" RadiusX="3" RadiusY="3">
              <Rectangle.Fill>
                <SolidColorBrush x:Name="myAnimatedBrush" Color="Red" />
              </Rectangle.Fill>
            </Rectangle>
            <Line X1="3" Y1="3" X2="12" Y2="12" Stroke="White" StrokeThickness="2"></Line>
            <Line X1="12" Y1="3" X2="3" Y2="12" Stroke="White" StrokeThickness="2"></Line>
          </Canvas>
        </ControlTemplate>
      </Button.Template>
    </Button>
  </Canvas>
</Window>
//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Input;

namespace NonRectangularWindowSample {

  public partial class NonRectangularWindow : Window {

    public NonRectangularWindow() {
      InitializeComponent();
    }

    void NonRectangularWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
      this.DragMove();
    }

    void closeButtonRectangle_Click(object sender, RoutedEventArgs e) {
      this.Close();
    }
  }
}

   
    
     


Order of precedence for sizing-related properties that are implemented by Window.

image_pdfimage_print


   
  

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="SizingPrecedenceSampleCSharp.MainWindow"
    Title="Sizing Sample" Height="300" Width="300" SizeToContent="WidthAndHeight">
  <StackPanel>
      <CheckBox Name="setWSCB" Content="Set WindowState?"/>
      <ComboBox Name="wsLB" IsEnabled="{Binding ElementName=setWSCB,Path=IsChecked}">
        <ComboBoxItem IsSelected="True">Minimized</ComboBoxItem>
        <ComboBoxItem>Maximized</ComboBoxItem>
        <ComboBoxItem>Normal</ComboBoxItem>
      </ComboBox>
      <CheckBox Name="setMinWidthCB" Content="Set MinWidth?"/>
      <TextBox Name="minWidthTB" IsEnabled="{Binding ElementName=setMinWidthCB,Path=IsChecked}">500</TextBox>
      <CheckBox Name="setMinHeightCB" Content="Set MinHeight?"/>
      <TextBox Name="minHeightTB" IsEnabled="{Binding ElementName=setMinHeightCB,Path=IsChecked}">500</TextBox>
      <CheckBox Name="setSTCCB" Content="Set SizeToContent?"/>
      <ComboBox Name="stcLB" IsEnabled="{Binding ElementName=setSTCCB,Path=IsChecked}">
        <ComboBoxItem IsSelected="True">Manual</ComboBoxItem>
        <ComboBoxItem>Width</ComboBoxItem>
        <ComboBoxItem>Height</ComboBoxItem>
        <ComboBoxItem>WidthAndHeight</ComboBoxItem>
      </ComboBox>
      <CheckBox Name="setMaxWidthCB" Content="Set MaxWidth?"></CheckBox>
      <TextBox Name="maxWidthTB" IsEnabled="{Binding ElementName=setMaxWidthCB,Path=IsChecked}">800</TextBox>
      <CheckBox Name="setMaxHeightCB" Content="Set MaxHeight?"></CheckBox>
      <TextBox Name="maxHeightTB" IsEnabled="{Binding ElementName=setMaxHeightCB,Path=IsChecked}">800</TextBox>
      <CheckBox Name="setWidthCB" Content="Set Width?"></CheckBox>
      <TextBox Name="widthTB" IsEnabled="{Binding ElementName=setWidthCB,Path=IsChecked}">700</TextBox>
      <CheckBox Name="setHeightCB" Content="Set Height?"></CheckBox>
      <TextBox Name="heightTB" IsEnabled="{Binding ElementName=setHeightCB,Path=IsChecked}">700</TextBox>
      <Button Click="showWindowButton_Click">Show Window</Button>
  </StackPanel>
</Window>

//File:Window.xaml.cs

using System;
using System.Windows;

namespace SizingPrecedenceSampleCSharp
{
    public partial class MainWindow : System.Windows.Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }

        void showWindowButton_Click(object sender, RoutedEventArgs e)
        {
            MainWindow sw = new MainWindow();

            if (this.setWSCB.IsChecked == true) sw.WindowState = (WindowState)Enum.Parse(typeof(WindowState), this.wsLB.Text);
            if (this.setMinWidthCB.IsChecked == true) sw.MinWidth = double.Parse(this.minWidthTB.Text);
            if (this.setMinHeightCB.IsChecked == true) sw.MinHeight = double.Parse(this.minHeightTB.Text);
            if (this.setMaxWidthCB.IsChecked == true) sw.MaxWidth = double.Parse(this.maxWidthTB.Text);
            if (this.setMaxHeightCB.IsChecked == true) sw.MaxHeight = double.Parse(this.maxHeightTB.Text);
            if (this.setWidthCB.IsChecked == true) sw.Width = double.Parse(this.widthTB.Text);
            if (this.setHeightCB.IsChecked == true) sw.Height = double.Parse(this.heightTB.Text);
            if (this.setSTCCB.IsChecked == true) sw.SizeToContent = (SizeToContent)Enum.Parse(typeof(SizeToContent), this.stcLB.Text);

            sw.Show();
        }
    }
}