如何在所有窗口上添加一个通用控件?

4
我想创建一个样式来修改所有的窗口并添加一些控件。
我的窗口看起来像这样: wpf window 我想让它看起来像这样: wpf window with style 通过应用一个样式,这样我就不需要在所有窗口中复制代码了。
我该怎么做呢?
1个回答

6

1.创建资源字典

<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. 将字典添加到每个 window.resource 并设置样式

<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>

3
几乎完美了,我只需要保留窗口内容。我猜我可以使用“ContentPresenter”来实现? - BrunoLM
1
没错 @BrunoLM,也许你可以尝试使用 <DockPanel Background="{TemplateBinding Background}" LastChildFill="True"> <!--button etc here with DockPanel.Dock="Top" --> <Grid DockPanel.Dock="Top"> <Label Foreground="Black" Content="notifications container"/> </Grid> <ContentPresenter/> </DockPanel> - pushpraj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接