我想创建一个样式来修改所有的窗口并添加一些控件。
我的窗口看起来像这样:
我想让它看起来像这样:
通过应用一个样式,这样我就不需要在所有窗口中复制代码了。
我该怎么做呢?
我的窗口看起来像这样:
![wpf window](https://istack.dev59.com/KlwKF.webp)
![wpf window with style](https://istack.dev59.com/2RUvF.webp)
我该怎么做呢?
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>
<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