How to add a common control to all my windows?

I want to create a style for changing all my windows and add some controls to it.

My window is as follows:

wpf window

I want to do this:

wpf window with style

Applying style, so I don’t need to duplicate code in all my windows.

How can i do this?

+4
source share
1 answer

1.creat resource dictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="winStyle" TargetType="{x:Type Window}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="{x:Type Window}">
                    <DockPanel  Background="{TemplateBinding Background}">
                        <Grid Height="200" DockPanel.Dock="Top"/>
                        <Grid>
                            <Label Foreground="Black" Content="notifications container"/>
                        </Grid>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

2.add dictionary for each window.resource and set styles

<Window x:Class="WpfApplication21.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Style="{DynamicResource winStyle}">
    <Window.Resources>
        <ResourceDictionary Source="Dictionary1.xaml" />
    </Window.Resources>
</Window>
+6
source

All Articles