Creating the main panel and add to Window in Code


   
  



<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CommandHandlerProcedural"
    >
</Window>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            
            StackPanel MainStackPanel = new StackPanel();
            this.AddChild(MainStackPanel);

            Button CommandButton = new Button();
            CommandButton.Command = ApplicationCommands.Open;
            CommandButton.Content = "Open (KeyBindings: Ctrl-R, Ctrl-0)";
            MainStackPanel.Children.Add(CommandButton);

            CommandBinding OpenCmdBinding = new CommandBinding(ApplicationCommands.Open,OpenCmdExecuted,OpenCmdCanExecute);

            this.CommandBindings.Add(OpenCmdBinding);

            KeyBinding OpenCmdKeyBinding = new KeyBinding(ApplicationCommands.Open,Key.R,ModifierKeys.Control);

            this.InputBindings.Add(OpenCmdKeyBinding);
        }

        void OpenCmdExecuted(object target, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("The command has been invoked.");
        }

        void OpenCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
    }
}

   
    
     


Window Closing and Closed event


   
  

<Window x:Class="ApplicationShutdownSample.ChildWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Application Shutdown Sample" Closing="ChildWindow_Closing" Closed="ChildWindow_Closed">
    <Grid>
    </Grid>
</Window>

//File:Window.xaml.cs
using System;
using System.ComponentModel;
using System.Windows;

namespace ApplicationShutdownSample
{
    public partial class ChildWindow : Window
    {
        public ChildWindow()
        {
            InitializeComponent();
        }

        void ChildWindow_Closing(object sender, CancelEventArgs e)
        {
            Console.WriteLine("Closing");
            MessageBoxResult result = MessageBox.Show("Allow Shutdown?", "Application Shutdown Sample", MessageBoxButton.YesNo, MessageBoxImage.Question);
            e.Cancel = (result == MessageBoxResult.No);
        }

        void ChildWindow_Closed(object sender, EventArgs e)
        {
            Console.WriteLine("Closed");
        }
    }
}

   
    
     


Use WindowState to make full screen window, change resize mode to NoReSize

   
  
<Window x:Class="_360Timer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Concentric Rings" Width="910" Height="512">
  <Canvas Name="MainCanvas" Background="#FFE0E0E0"/>
</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;
using System.Windows.Media.Animation;


namespace _360Timer
{

    public partial class Window1 : Window
    {
        System.Windows.Threading.DispatcherTimer frameTimer;
        private int lastTick;
        private Random rand;

        public Window1()
        {
            InitializeComponent();

            this.WindowState = WindowState.Maximized;
            this.WindowStyle = WindowStyle.None;
            this.ResizeMode = ResizeMode.NoResize;
        }

        void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
                this.Close();
        }
    }
}

   
    
     


Close a window with Escape key pressed


   
  

<Window x:Class="_360Timer.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Concentric Rings" Width="910" Height="512">
  <Canvas Name="MainCanvas" Background="#FFE0E0E0"/>
</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;
using System.Windows.Media.Animation;


namespace _360Timer
{

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.Show();

            this.KeyDown += new System.Windows.Input.KeyEventHandler(Window1_KeyDown);
        }

        void Window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
                this.Close();
        }

    }
}

   
    
     


Animated Video Window


   
  

<Window x:Class="SoundAndVideo.AnimatedVideoWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AnimatedVideoWindow" Height="450" Width="400">

  <Window.Resources>
    <Storyboard x:Key="MediaStoryboardResource">
      <MediaTimeline Storyboard.TargetName="media" Source="test.mpg" FillBehavior="HoldEnd"
                      RepeatBehavior="Forever"></MediaTimeline>
      <DoubleAnimation Storyboard.TargetName="media" Storyboard.TargetProperty="(MediaElement.RenderTransform).(RotateTransform.Angle)"
    To="360" Duration="0:0:2" RepeatBehavior="Forever"></DoubleAnimation>
    </Storyboard>
  </Window.Resources>

  <StackPanel>
    <StackPanel.Triggers>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdPlay">
        <EventTrigger.Actions>
          <BeginStoryboard Name="MediaStoryboard" Storyboard="{StaticResource MediaStoryboardResource}"/>
        </EventTrigger.Actions>
      </EventTrigger>
      <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="cmdStop">
        <EventTrigger.Actions>
          <StopStoryboard BeginStoryboardName="MediaStoryboard"/>
        </EventTrigger.Actions>
      </EventTrigger>      
    </StackPanel.Triggers>

      <Button Name="cmdPlay">Play (Declarative)</Button>
      <Button Name="cmdStop">Stop (Declarative)</Button>
      <Button Click="cmdPlayCode_Click">Play (Code)</Button>
    <MediaElement Name="media" Grid.Row="1" Stretch="None" RenderTransformOrigin="0.5,0.5" >
      <MediaElement.RenderTransform>
        <RotateTransform></RotateTransform>
      </MediaElement.RenderTransform>
    </MediaElement>
  </StackPanel>

</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.Windows.Media.Animation;

namespace SoundAndVideo
{
    public partial class AnimatedVideoWindow : System.Windows.Window
    {
        public AnimatedVideoWindow()
        {
            InitializeComponent();
        }
        private void cmdPlayCode_Click(object sender, RoutedEventArgs e)
        {
            MediaTimeline timeline = new MediaTimeline(new Uri("test.mpg", UriKind.Relative));            
            timeline.RepeatBehavior = RepeatBehavior.Forever;

            MediaClock clock = timeline.CreateClock();
            MediaPlayer player = new MediaPlayer();
            player.Clock = clock;

            VideoDrawing videoDrawing = new VideoDrawing();
            videoDrawing.Rect = new Rect(150, 0, 100, 100);
            videoDrawing.Player = player;

            DrawingBrush brush = new DrawingBrush(videoDrawing);
            this.Background = brush;

            clock.Controller.Begin();
        }
    }
}

   
    
     


Transparent Window


   
  
<Window x:Class="Windows.ModernWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ModernWindow" Height="300" Width="300"
        AllowsTransparency="True" Background="Transparent"
        WindowStyle="None" ResizeMode="CanResizeWithGrip">
   <Border Width="Auto" Height="Auto" Name="windowFrame"
          BorderBrush="#395984"
          BorderThickness="1"
          CornerRadius="0,20,30,40" >
    <Border.Background>
      <LinearGradientBrush >
        <GradientBrush.GradientStops>
          <GradientStopCollection>
            <GradientStop Color="#E7EBF7" Offset="0.0"/>
            <GradientStop Color="#CEE3FF" Offset="0.5"/>
  
          </GradientStopCollection>
        </GradientBrush.GradientStops>
      </LinearGradientBrush>
    </Border.Background>
    <StackPanel>
      <TextBlock Text="Title Bar" Margin="1" Padding="5" MouseLeftButtonDown="titleBar_MouseLeftButtonDown"></TextBlock>
      <Button Click="cmdClose_Click">Close</Button>
      <Rectangle Cursor="SizeWE" 
                 MouseLeftButtonDown="window_initiateWiden"
                 MouseLeftButtonUp="window_endWiden"
                 MouseMove="window_Widen"/>

      </StackPanel>
    
    
  </Border>
</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 ModernWindow : System.Windows.Window
    {
        public ModernWindow()
        {
            InitializeComponent();
        }

        bool isWiden = false;
        private void window_initiateWiden(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isWiden = true;
        }
        private void window_endWiden(object sender, System.Windows.Input.MouseEventArgs e)
        {
            isWiden = false;

            Rectangle rect = (Rectangle)sender;
            rect.ReleaseMouseCapture();
        }

        private void window_Widen(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Rectangle rect = (Rectangle)sender;
            if (isWiden)
            {                
                rect.CaptureMouse();
                double newWidth = e.GetPosition(this).X + 5;
                if (newWidth > 0) this.Width = newWidth;
            }            
        }

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

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

   
    
     


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