wpfwindow-style

How could providing Parent Window as Pattern - WPF


Consider I have 4 Window on my project and I try to provide specific Closing button and one Title

How could I make a object of window and all of window use it as Pattern.

Here is Example of what we have for pattern window:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        WindowStyle="None" AllowsTransparency="True" >
<Grid>
<Button Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
<TextBlock VerticalAlignment="Top" HorizontalAlignment="Center" X:Name="WindowTitle/>
</Grid>
</Window>

How could I use for all of my Window as pattern. Thanks


Solution

  • Actually, there is no need to write a parent window. You can use Style and Template instead. It's more convenient and is recommended by Microsoft WPF Team.

    enter image description here

    Write the code below into your App.xaml and you'll get the picture above:

    <Application x:Class="Walterlv.Demo.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 .........>
        <Application.Resources>
            <Style x:Key="Style.Window.Default" TargetType="Window">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="Window">
                            <Grid Background="{TemplateBinding Background}">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="40"/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <Button Grid.Row="0" Content="Close" Height="40" VerticalAlignment="Top" HorizontalAlignment="Right"/>
                                <TextBlock Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
                                           Text="{TemplateBinding Title}"/>
                                <Border Grid.Row="1" BorderThickness="{TemplateBinding BorderThickness}"
                                        BorderBrush="{TemplateBinding BorderBrush}">
                                    <!-- This is the container to host your Window.Content -->
                                    <ContentPresenter/>
                                </Border>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Application.Resources>
    </Application>
    

    And you can use only one property Style to share such a "pattern":

    <Window x:Class="Walterlv.Demo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Style="{StaticResource Style.Window.Default}">
    
    </Window>
    

    You can define different kinds of styles in the App.xaml file and select anyone in your XxxWindow.xaml you need.