wpf 如何在我的所有 Windows 上添加通用控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25298599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a common control on all my Windows?
提问by BrunoLM
I want to create a style to modify all my windows and add some controls to it.
我想创建一个样式来修改我的所有窗口并向其添加一些控件。
My Window looks like this:
我的窗口看起来像这样:


I want to make it look like this:
我想让它看起来像这样:


By applying a style so I don't have to duplicate code across all my windows.
通过应用样式,我不必在所有窗口中复制代码。
How can I do that?
我怎样才能做到这一点?
回答by Rang
1.creat a resource dictionary
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.add the dictionary to every window.resource and set styles
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>

