Rac*_*hel 5 c# wpf templates styles window
我想创建一些我常用的WPF控件的库,其中一个控件是CustomWindow继承自Window类的控件.如何让我CustomWindow使用库中定义的默认外观?
我可以替换
<Window x:Class="..." />
Run Code Online (Sandbox Code Playgroud)
同
<MyControls:CustomWindow x:Class="..." />
Run Code Online (Sandbox Code Playgroud)
它适用于窗口行为,但不适用于出现.
编辑
这是我到目前为止的简化版本:
自定义窗口控件.位于控制库中.
public class CustomChromeWindow: Window
{
static CustomChromeWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomChromeWindow),
new FrameworkPropertyMetadata(typeof(CustomChromeWindow)));
}
}
Run Code Online (Sandbox Code Playgroud)
窗口风格.位于Generic.xaml中,是控件库的Themes文件夹中的ResourceDictionary
<Style TargetType="local:CustomChromeWindow">
<Setter Property="WindowStyle" Value="None" />
<Setter Property="Background" Value="Red" />
</Style>
Run Code Online (Sandbox Code Playgroud)
测试窗口.引用控件库的单独项目的启动窗口
<local:CustomChromeWindow
x:Class="MyControlsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyControls;assembly=MyControls"
Title="MainWindow" Height="350" Width="525"
>
<Grid>
<TextBlock Text="This is a Test" />
</Grid>
</local:CustomChromeWindow>
Run Code Online (Sandbox Code Playgroud)
我最终获得的是一个带有常规WindowStyle和黑色背景的窗口.
使用这个xaml:
<Window x:Class="MyNamespace.CustomWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyControls="MyNamespace">
<Window.Style>
<Style TargetType="MyControls:CustomWindow">
...
</Style>
</Window.Style>
<ContentPresenter />
</Window>
Run Code Online (Sandbox Code Playgroud)
您可能想为窗口设计一个新主题。如果是这样,请将以下主题放入(您的库)\Themes\Generic.xaml 资源文件中:
<Style TargetType="{x:Type MyControls:CustomWindow}">
<Setter Property="WindowStyle" Value="None" />
<Setter Property="AllowsTransparency" Value="True" />
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MyControls:CustomWindow}">
<Border>
...
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)