Skip to content

Instantly share code, notes, and snippets.

@JoshClose
Created November 15, 2011 16:51
Show Gist options
  • Save JoshClose/1367576 to your computer and use it in GitHub Desktop.
Save JoshClose/1367576 to your computer and use it in GitHub Desktop.
Creating a Common Window in WPF
<ControlTemplate TargetType="Window" x:Key="WindowTemplate">
<aero:SystemDropShadowCrome CornerRadius="10" Margin="10">
<Border BorderThickness="1" BorderBrush="Black" Background="White"
CornerRadius="10">
<Border Height="40" Background="#01000000" VerticalAlignment="Top"
CornerRadius="10,10,0,0" MouseLeftButtonDown="DragWindow">
<ContentPresenter />
</Border>
</Border>
</aero:SystemDropShadowCrome>
</ControlTemplate>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Windows.Themes;
namespace CommonWindow
{
public abstract class WindowBase : Window
{
public WindowBase()
{
Initialized += WindowBaseInitialized;
}
private void WindowBaseInitialized( object sender, System.EventArgs e )
{
SetResourceReference( Window.StyleProperty, "Window" );
ResourceDictionary resource = new ResourceDictionary();
resource.Source = new Uri( "pack://application:,,,/WindowResource.xaml" );
this.Resources.MergedDictionaries.Add( resource );
SystemDropShadowChrome dropShadow = new SystemDropShadowChrome();
dropShadow.SetResourceReference( Control.StyleProperty, "WindowShadow" );
Border windowBorder = new Border();
windowBorder.SetResourceReference( Control.StyleProperty, "WindowBorder" );
dropShadow.Child = windowBorder;
Grid grid = new Grid();
grid.RowDefinitions.Add( new RowDefinition() { Height = new GridLength( 40.0 ) } );
grid.RowDefinitions.Add( new RowDefinition() );
windowBorder.Child = grid;
Border headerBorder = new Border();
headerBorder.SetResourceReference( Control.StyleProperty, "HeaderBorder" );
headerBorder.MouseLeftButtonDown += DragWindow;
grid.Children.Add( headerBorder );
Grid.SetRow( headerBorder, 0 );
ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.Content = this.Content;
grid.Children.Add( contentPresenter );
Grid.SetRow( contentPresenter, 1 );
this.Content = dropShadow;
}
private void DragWindow( object sender, MouseButtonEventArgs e )
{
DragMove();
}
}
}
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:aero="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<Style TargetType="Window" x:Key="Window">
<Setter Property="AllowsTransparency" Value="True" />
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style TargetType="aero:SystemDropShadowChrome" x:Key="WindowShadow">
<Setter Property="CornerRadius" Value="10" />
<Setter Property="Margin" Value="10" />
</Style>
<Style TargetType="Border" x:Key="WindowBorder">
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Background" Value="White" />
<Setter Property="CornerRadius" Value="10" />
</Style>
<Style TargetType="Border" x:Key="HeaderBorder">
<Setter Property="Grid.Row" Value="0" />
<Setter Property="Background" Value="#01000000" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="CornerRadius" Value="10,10,0,0" />
</Style>
</ResourceDictionary>
namespace CommonWindow
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : WindowBase
{
public Window1()
{
InitializeComponent();
}
}
}
<local:WindowBase x:Class="CommonWindow.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CommonWindow"
Title="Window1" Height="300" Width="300">
<Label Content="blah" />
</local:WindowBase>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment