My Tab control has a resource like this:
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="White" />
<Setter TargetName="Image" Property="Visibility" Value="Visible" />
<Setter TargetName="Border" Property="BorderBrush" Value="Gray" />
<Setter TargetName="TextBlock" Property="Foreground" Value="Black" />
<Setter TargetName="Border" Property="Margin" Value="-2,0,2,-1" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True" SourceName="Border" >
<Setter TargetName="Border" Property="Background" Value="White" />
<Setter TargetName="Border" Property="BorderBrush" Value="DarkGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid Background="WhiteSmoke" Name="Grid1">
<Grid.RowDefinitions>
<RowDefinition Height="5"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Canvas Background="{StaticResource {x:Static SystemColors.ActiveCaptionBrushKey}}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
How can I add a control like "Label" to Grid(in Resource with name of "Grid1") of my TabControl?
Try to search the grid in the VisualTree and add then your control to it. You can use the following helper function to find the grids and check then for the name. Otherwise you can also extend the code for a easier search (by adding a name parameter).
void FindChildFrameworkElementsOfType<T>(DependencyObject parent,IList<T> list) where T: FrameworkElement{
DependencyObject child;
for(int i=0;i< VisualTreeHelper.GetChildrenCount(parent);i++){
child = VisualTreeHelper.GetChild(parent, i);
if (child is T) {
list.Add((T)child);
}
FindChildFrameworkElementsOfType<T>(child,list);
}
}
Something like:
List<Grid> list=new List<Grid>();
FindCHildFrameworkElementsOfType<Grid>(this,list)
foreach(Grid grid in list){
if(grid.Name=="Grid1"){
// Add here your control
break;
}
}
You can add a Loaded event handler:
<Grid Background="WhiteSmoke" Name="Grid1" Loaded="Grid1_Loaded">
and populate and/or record the reference to the grid in the handler:
private Grid grid1;
private void grid1_Loaded(object sender, RoutedEventArgs e)
{
grid1 = sender as Grid;
// add label, etc.
}
Special case:
event handlers in a resource dictionary
Related
The radio button and checkbox will be adding to window dynamically which based from how many data will be on database.
I have try some approach but cannot get what I need. Below are the code that will be perform to add either radio button or checkbox:-
private void ScreenSubList_Loaded(object sender, RoutedEventArgs e)
{
try
{
strSubList LastGroupName = strSubList.Empty;
foreach (var SubList in ProductSubList)
{
StackPanel StackGroup = new StackPanel() { Orientation = Orientation.Vertical };
if (SubList.GroupName != LastGroupName)
{
Label LabelGroupName = new Label() { Content = SubList.GroupName.ToUpper() };
ScaleTransform ElementScaleTransform = new ScaleTransform(6, 6);
LabelGroupName.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(LabelGroupName);
LastGroupName = SubList.GroupName;
}
if (SubList.GroupType == 0)
{
RadioButton rb = new RadioButton();
if (SubList.SubListItem != null)
{
StackPanel StackItem = new StackPanel() { Orientation = Orientation.Horizontal };
foreach (var SubListitem in SubList.SubListItem)
{
rb.Tag = SubListitem.ItemID;
rb.Name = "SubList" + SubListitem.ItemID;
rb.Content = SubListitem.ItemName;
rb.HorizontalContentAlignment = HorizontalAlignment.Left;
rb.VerticalContentAlignment = VerticalAlignment.Center;
rb.GroupName = SubList.GroupName;
ScaleTransform ElementScaleTransform = new ScaleTransform(5, 5);
rb.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(rb);
}
}
}
else if (SubList.GroupType == 1)
{
CheckBox cbx = new CheckBox();
if (SubList.SubListItem != null)
{
StackPanel StackItem = new StackPanel() { Orientation = Orientation.Horizontal };
foreach (var SubListitem in SubList.SubListItem)
{
cbx.Tag = SubListitem.ItemID;
cbx.Name = "SubList" + SubListitem.ItemID;
cbx.Content = SubListitem.ItemName;
cbx.HorizontalContentAlignment = HorizontalAlignment.Left;
cbx.VerticalContentAlignment = VerticalAlignment.Center;
ScaleTransform ElementScaleTransform = new ScaleTransform(5, 5);
cbx.RenderTransform = ElementScaleTransform;
StackGroup.Children.Add(cbx);
}
}
}
ScreenSubListredient.StackSubList.Children.Add(StackGroup);
}
}
catch (Exception ex)
{
App.LogEvents($"Exception on ScreenSubList_Loaded. Message-{ex.Message}. Stack Trace-{ex.StackTrace}", System.Diagnostics.EventLogEntryType.Error);
}
}
I also play around with Blend to see the outcome from what I have test. Some of the test are:-
1. ScaleTransform the radiobutton and checkbox before adding to stackpanel
2. Group default radiobutton and checkbox into view.
Problem on the testing:
ScaleTransform cannot stack to stackpanel accordingly
Viewbox is having different size depend on the text length. If radiobutton or checkbox got lesser text, it will going big to stretch inside stackpanel. Manually adjusting the width and height make the viewbox and content look distort and will be a lot of work to calculate the Width and Height that will view the text at same size.
Below are the image sample as the output:
On most left, I just change the text size, text bigger but bullet options still tiny.
On middle, the options using Viewbox. All font is at default size Segoe UI 9pt
On most right, the usage of ScaleTransform. It was likely the middle pointer is stack vertically on panel. And it's unsure how to control base from latest size of the radiobutton and checkbox since on Height & Width properties, it show the default size before Transform.
What I need is actually a radio button and check box that have it's bullet follow the size of the text. I've go through internet for this for a weeks but not find any solutions to my situations.
RadioButton and CheckBox have a hardcoded bullet size in their Templates. I have taken RadioButton template and CheckBox template and modified bullet sizes. I bind their Height and Width to content ActualHeight:
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}"
Complete templates:
<Style x:Key="{x:Type RadioButton}" TargetType="{x:Type RadioButton}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource RadioButtonFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Grid Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}" >
<Ellipse x:Name="Border"
Fill="{StaticResource NormalBrush}"
StrokeThickness="1"
Stroke="{StaticResource NormalBorderBrush}" />
<Ellipse x:Name="CheckMark"
Margin="4"
Fill="{StaticResource GlyphBrush}" />
</Grid>
</BulletDecorator.Bullet>
<ContentPresenter Name="PART_Content"
Margin="4,0,0,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource DarkBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="Stroke" Value="{StaticResource GlyphBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Fill" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="Stroke" Value="#40000000" />
<Setter Property="Foreground" Value="#80000000"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="{x:Type CheckBox}" TargetType="CheckBox">
<Setter Property="IsThreeState" Value="True"/>
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckBoxFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator Background="Transparent">
<BulletDecorator.Bullet>
<Border x:Name="Border"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Height="{Binding ActualHeight, ElementName=PART_Content}"
CornerRadius="0"
Background="{StaticResource NormalBrush}"
BorderThickness="1"
BorderBrush="{StaticResource NormalBorderBrush}">
<Viewbox Margin="2">
<Path
Width="7" Height="7"
x:Name="CheckMark"
StrokeStartLineCap="Flat"
StrokeEndLineCap="Flat"
SnapsToDevicePixels="False"
Stroke="{StaticResource GlyphBrush}"
StrokeThickness="2"
Data="M 1 1 L 6 6 M 1 6 L 6 1" />
</Viewbox>
</Border>
</BulletDecorator.Bullet>
<ContentPresenter Margin="4,0,0,0" Name="PART_Content"
VerticalAlignment="Center"
HorizontalAlignment="Left"
RecognizesAccessKey="True"/>
</BulletDecorator>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="false">
<Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
</Trigger>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter TargetName="CheckMark" Property="Data" Value="M 3 3 L 3 4 L 4 4 L 4 3 Z" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DarkBrush}" />
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="Border" Property="Background" Value="{StaticResource PressedBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource PressedBorderBrush}" />
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}" />
<Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBorderBrush}" />
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I want to create custom visual style for my buttons.
I need a simple working example of how to override default visual style of a button. As well as a simple explanation of how to apply it.
I want to get something working, so I can start from there and experiment my way further.
I've tried to add a new recourse dictionary as follows:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="mstyle" TargetType="Button">
<Setter Property="FontWeight" Value="Bold" />
</Style>
</ResourceDictionary>
after that I've created some new button in runtime and tried to apply this style to it:
Dim MyButton As New Button
Dim st As New Style
st = Application.Current.FindResource("mstyle")
MyButton.Style = st
When I try to run this, I get an error that the recourse 'mstyle' could not be found.
You don't in most cases need any code behind to do that all what you need is to define a custom style that target your button in the resource dictionary or in the window resource here an example :
<Style x:Key="DarkStyleButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#373737" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontSize" Value="12" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="4" Background="{TemplateBinding Background}">
<Grid>
<ContentPresenter x:Name="MyContentPresenter" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#E59400" />
<Setter Property="Foreground" Value="White" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" Value="White" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="Gray" />
<Setter Property="Foreground" Value="LightGray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
first set the value for the properties you want to customize,
then set the button template and don't forget to add the
ContentPresenter that will hold the button content
finally define triggers to handle the mouse over, click and what ever
else you want to set a custom look when it triggers (for example the
desable/enabled )
To use that style here how
<Button x:Name="BrowseButton" Margin="5" Style="{StaticResource DarkStyleButton}" ToolTip="tooltip about the button">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="../BrowseImage.png"/>
<TextBlock Text="Browse" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="5"></TextBlock>
</StackPanel>
</Button.Content>
</Button>
I'm struggling with this below problem. my style file in resource directory. but it can't apply click method to context menu item. it's showing below this error. please help me how can i achieve this.
Error : "'Failed to create a 'Click' from the text 'OnMenuItemClick'.' Line number '10' and line position '35'."
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:src="clr-namespace:System;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ObjectDataProvider x:Key="date" ObjectType="{x:Type src:DateTime}"/>
<Style x:Key="ContextMenuStyle1" TargetType="{x:Type ContextMenu}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="BorderBrush" Value="Transparent"/>
</Style>
<ContextMenu x:Key="ListViewContext" Style="{StaticResource ContextMenuStyle1}">
<MenuItem Header="Create" Click="OnMenuItemClick" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"></MenuItem>
</ContextMenu>
<ContextMenu x:Key="GridItemContext" Style="{StaticResource ContextMenuStyle1}">
<MenuItem Header="Modify" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
<MenuItem Header="Delete" CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}"/>
</ContextMenu>
<Style x:Key="ListViewGrid" TargetType="{x:Type ListView}">
<Setter Property="BorderBrush" Value="#FFDFDFE2"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Background" Value="#faf2f2"/>
<Setter Property="ContextMenu" Value="{StaticResource ListViewContext}"/>
</Style>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="ContextMenu" Value="{StaticResource GridItemContext}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Border CornerRadius="0" SnapsToDevicePixels="True" >
<Border Name="InnerBorder" CornerRadius="0" BorderThickness="0,0,0,1" BorderBrush="#FFDFDFE2">
<Grid Background="#FFEFEFEF" Name="Trg" Height="20">
<GridViewRowPresenter />
</Grid>
</Border>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Trg" Property="Background" Value="#FFDFDFE2" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Trg" Property="Background" Value="#FFDFDFE2" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When I tried the above code, the error I got was:
Error 1 'ResourceDictionary' root element requires a x:Class attribute
to support event handlers in the XAML file. Either remove the event
handler for the Click event, or add a x:Class attribute to the root
element. Line 10 Position 35
This is how I fixed it:
1) Added x:Class attribute to the ResourceDictionary:
x:Class="WpfApplication4.Dictionary1"
2) Added a C# class file to the project, Code below:
public partial class Dictionary1 : ResourceDictionary
{
public Dictionary1()
{
InitializeComponent();
}
void OnMenuItemClick(object sender, RoutedEventArgs e)
{
}
}
And then, the project built fine.
I have the following button style that has a cross in it and is used for a close button on a TabItem
<Button Grid.Column="2"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DockPanel.Dock="Right"
AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.CloseWorkspaceCommand}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid Background="{TemplateBinding Background}">
<Path x:Name="ButtonPath"
Margin="3"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="M0,0 L1,1 M0,1 L1,0"
SnapsToDevicePixels="True"
Stretch="Uniform"
Stroke="{DynamicResource CloseButtonStroke}"
StrokeEndLineCap="Flat"
StrokeStartLineCap="Flat"
StrokeThickness="2"/>
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
Value="false" />
<Condition Binding="{Binding IsMouseOver, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TabItem}}"
Value="false" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Hidden" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Visibility" Value="Hidden" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background"
Value="{DynamicResource CloseButtonBackgroundHighlighted}" />
<Setter TargetName="ButtonPath"
Property="Stroke"
Value="{DynamicResource CloseButtonStrokeHighlighted}" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background"
Value="{DynamicResource CloseButtonBackgroundPressed}" />
<Setter TargetName="ButtonPath"
Property="Stroke"
Value="{DynamicResource CloseButtonStroke}" />
<Setter TargetName="ButtonPath"
Property="Margin"
Value="2.5,2.5,1.5,1.5" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Button.Style>
</Button>
This produces
I want to add a CheckButton styled to show a pin next to the close button like VS2012. I can do this no problem, but I want to place the CheckBox inside the same button template above so I get highlighting when the mouse is over the area. The problem is the button above contains the Path that draws the 'X'.
Is there a was I can make the button style above generic so I can include either the 'X' Path or the CheckBox, without replicating all the trigger stuff?
Thanks for your time.
I would approach this by creating my button class like ToolBarButton below and have a dependency property ButtonType. I will set ButtonType on the ToolBarButton.
public enum ButtonType
{
Close =0,
Pin
}
public class ToolBarButton : Button
{
public ButtonType ButtonType
{
get { return (ButtonType)this.GetValue(ButtonTypeProperty); }
set { this.SetValue(ButtonTypeProperty, value); }
}
public static readonly DependencyProperty ButtonTypeProperty = DependencyProperty.Register(
"ButtonType", typeof(ButtonType), typeof(ToolBarButton), new PropertyMetadata(ButtonType.Close));
}
in xaml:
<local:ToolBarButton ButtonType="Pin"/>
Then I will add the trigger in style to check for buttontype and apply the controltemplate containing checkbox
<Style.Triggers>
<Trigger Property="ButtonType" Value="Pin">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<CheckBox/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
How would I create a common window look in WPF? I'm not talking about just styling the window, I'm mean having a window that has a border, grid, and some other things in it.
Thanks.
You can create a ControlTemplate for the window. Here is a pretty basic example that has some controls and triggers. You can easily add more elements to make it fit your needs.
<ControlTemplate x:Key="MyWindowTemplate" TargetType="{x:Type Window}">
<Border x:Name="WindowBorder" Style="{DynamicResource WindowBorderStyle}">
<Grid>
<Border Margin="4,4,4,4" Padding="0,0,0,0" x:Name="MarginBorder">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<ResizeGrip Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" x:Name="WindowResizeGrip"
VerticalAlignment="Bottom" />
</Grid>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
<Condition Property="WindowState" Value="Normal"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
<Setter Property="Margin" TargetName="MarginBorder" Value="4,4,4,20" />
</MultiTrigger>
<Trigger Property="WindowState" Value="Maximized">
<Setter Property="CornerRadius" TargetName="WindowBorder" Value="0,0,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
You can use this ControlTemplate by setting the template property of the Window:
Template="{StaticResource MyWindowTemplate}"
You will want to use this in conjunction with a style like this:
<Style x:Key="MyWindowStyle" TargetType="{x:Type Window}">
<Setter Property="AllowsTransparency" Value="False" />
<Setter Property="WindowStyle" Value="SingleBorderWindow" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="Transparent" />
<Setter Property="ShowInTaskbar" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border>
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
And set the style on your Window like this:
Style="{StaticResource MyWindowStyle}"
What I ended up doing was creating a base class that created the UI code that I wanted in every window. This allows me to set events for controls and have the event subscription in the base class. If there is a better way of doing this, like using xaml, I'd like to know.
public WindowBase()
{
Initialized += WindowBase_Initialized;
}
private void WindowBase_Initialized( object sender, EventArgs e )
{
Border border = new Border();
border.SetResourceReference( Control.StyleProperty, "WindowBorder" );
border.Child = new ContentPresenter { Content = this.Content};
this.Content = border;
}