0

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

  • Is this what you're looking for? https://stackoverflow.com/questions/7174315/understanding-wpf-deriving-window-class – Aaron Nov 06 '17 at 19:55
  • @Aaron It's okay, thanks, But How should I having specific Button on pattern ? – Serareh Azimi Nov 06 '17 at 20:37
  • You would more than likely bind the base class button text and functionality to a property (or properties) in the base class. Then, when you set that property in child classes, the "OnPropertyChanged" notification will do whatever it needs to do to the button (add different handlers/bindings, remove unnecessary handlers/bindings, change text, etc)... Or, depending on how crazy it needs to be, you can create a 'base button' the same way you did your base window, and go totally nuts with it. – Aaron Nov 06 '17 at 20:40
  • If I understand you correctly. You may define a resource in a separate xaml file and use it in your other windows. check out "Merged resource dictionaries" section in the following link: https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/resourcedictionary-and-xaml-resource-references You need to define the resource and reuse it in your windows – Yitzchak Nov 06 '17 at 20:46

1 Answers1

0

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.

walterlv
  • 2,366
  • 13
  • 73