Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a common window look in WPF

Tags:

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.

like image 233
Josh Close Avatar asked Feb 19 '09 17:02

Josh Close


1 Answers

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}"
like image 115
John Myczek Avatar answered Oct 11 '22 18:10

John Myczek