Reputation: 85
I have created a windows control template so each window in my application will share the same menu at the top, copyright at the bottom and overall look and feel (colors, etc.). When I create a new window and apply the template to it, the new window looks like the template as expected but I cannot add anything new to the window. Anything I drag onto the new window is invisible (i.e. the XAML shows the control I added but I can't see it on the window). Is a template not the way to go? As I mentioned above I am trying to have each window use the same menu and have the same layout (colors, copyright text box at the bottom). Other than that, I want to be able to place other items relevant to each specific window on the "templated" window. Seems basic enough but as a VB 6 guy trying to grasp WPF (and definitely liking WPF) I am missing how to implement this. Any help is appreciated. Below is my code for the template and then how a new windows is set up with that template.
Template (in Application.Resources)
<Style x:Key="WindowTemplateMain" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<DockPanel Margin="0,0,-1.667,0.333" HorizontalAlignment="Stretch" Width="Auto">
<DockPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFAEC2EE" Offset="0"/>
<GradientStop Color="#FFFEFEFE" Offset="1"/>
</LinearGradientBrush>
</DockPanel.Background>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Time Entry" Name="mnu_TimeEntry" Click="mnu_TimeEntry" />
<Separator />
<MenuItem Header="_Logout" Click="mnu_LogoutClick"/>
<Separator />
<MenuItem Header="_Exit" Click="mnu_ExitClick"/>
</MenuItem>
<MenuItem Header="_Reports">
<MenuItem Header="_Report1" />
<MenuItem Header="_Report2" />
<MenuItem Header="_Report3" />
</MenuItem>
<MenuItem Header="_Administration">
<MenuItem Header="_Task1" />
<MenuItem Header="_Task2" />
<MenuItem Header="_Task3" />
</MenuItem>
</Menu>
<Grid Margin="0,0,0,0" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="74*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<TextBlock Style="{StaticResource MWB_Copyright}" Grid.Row="2" Grid.ColumnSpan="2" Margin="5,0,4.666,4" />
</Grid>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
New window with template "applied":
<Window x:Class="MWB_TimeKeeper_Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MWB_TimeKeeper"
mc:Ignorable="d"
Title="MWB TimeKeeper Main"
Style="{DynamicResource WindowTemplateMain}" >
<Button x:Name="MyButton" Content="MyButton" Height="100" Width="75"/>
</Window>
The button "MyButton" is what I dragged on from the toolbox and shows in the XAML code but doesn't show up on the window. Again, I am sure I am just not using the template correctly or need to use some type of style/template combination but I can't figure it out and have searched the web for awhile.
Thanks in advance for any help!
Upvotes: 0
Views: 58
Reputation: 4464
Chris W is right.
Add a content presenter to your content template as in code shown below. I have tested and the button is displayed.
N.B : I have removed the click events to test the code.
<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="WindowTemplateMain" TargetType="{x:Type Window}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<DockPanel Margin="0,0,-1.667,0.333" HorizontalAlignment="Stretch" Width="Auto">
<DockPanel.Background>
<LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0">
<GradientStop Color="#FFAEC2EE" Offset="0"/>
<GradientStop Color="#FFFEFEFE" Offset="1"/>
</LinearGradientBrush>
</DockPanel.Background>
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_Time Entry" Name="mnu_TimeEntry" />
<Separator />
<MenuItem Header="_Logout" />
<Separator />
<MenuItem Header="_Exit" />
</MenuItem>
<MenuItem Header="_Reports">
<MenuItem Header="_Report1" />
<MenuItem Header="_Report2" />
<MenuItem Header="_Report3" />
</MenuItem>
<MenuItem Header="_Administration">
<MenuItem Header="_Task1" />
<MenuItem Header="_Task2" />
<MenuItem Header="_Task3" />
</MenuItem>
</Menu>
<Grid Margin="0,0,0,0" Width="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="74*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="2" Grid.ColumnSpan="2" Margin="5,0,4.666,4" />
</Grid>
<ContentPresenter>
</ContentPresenter>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
Upvotes: 1