设置 WPF 用户控件的样式
Setting the style of a WPF UserControl
我知道我可以通过添加属性在控件中设置 UserControl
的样式:
Style="{StaticResource MyStyle}"
在我的 ResourceDictionary
中有一个样式如下所示:
<Style x:Key="MyStyle" TargetType="{x:Type UserControl}">
<Style.Resources>
<Style TargetType="Label">
<!-- Label Setters -->
</Style>
<Style TargetType="TextBox">
<!-- TextBox Setters -->
</Style>
</Style.Resources>
</Style>
但是有没有一种方法可以直接在 ResourceDictionary
中设置 UserControl
的样式,例如:
<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
基本上我的问题是,我可以将样式直接应用到控件而不是控件组件吗?
编辑:
我想要完成的是如下内容:
<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
<Setter Property="Background" Value="Black"/>
</Style>
<Style x:Key="{x:Type MyControl}" TargetType="{x:Type MyControl}" BasedOn="{StaticResource MyStyle}"/>
第二行将样式应用于应用程序中的所有控件,如果您对普通控件执行类似的操作,则此方法有效。
然而,这仅设置了 UserControl
的 Background
,因此我如何将相同的背景应用到其组件。
如何使用 UserControl
?
要设置所有控件的样式,请将您的 ResourceDictionary 添加到 App.xaml 的资源中。
<Application.Resources>
<!-- Your Resources for the whole application here -->
</Application.Resources>
如果您使用应用程序打开主窗口...
<Application ...
MainWindow="MainWindow">
或在启动事件期间...
<Application ...
MainWindow="MainWindow">
Startup="Application_Startup">
您的 MainWindow 的每个控件都可以使用这些资源。
要为特定用户控件设置样式,请看这里:
Set Style for user control
您可以像这样直接设置用户控件的样式:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style>
<Setter Property="local:MyControl.MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
或者像这样:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
用户控件资源中的默认样式也应该有效:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Resources>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Resources>
</UserControl>
您需要从定义的样式中删除 x:Key
,以便它可以普遍应用于与 TargetType
.
中定义的相同类型的所有控件
从 MSDN 引用 Style.TargetType Property:
Setting the TargetType property to the TextBlock type without setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you give the [...] Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.
在您的用户控件中 xaml 将样式放在资源标签内:
<UserControl>
<UserControl.Resources>
<Style ...</Style>
</UserControl.Resources>
//.. my other components
</UserControl>
特殊情况的死灵答案。如果用户控件是通过另一个 WPF 控件或 window 中的 DataTemplate 资源选择的,WPF 可能不会自动应用导入资源字典中的默认样式。但是,您可以在导入资源字典后应用命名样式资源。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Resources/ResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
<Binding Source="{StaticResource MyUserControlStyle}"></Binding>
</UserControl.Style>
我知道我可以通过添加属性在控件中设置 UserControl
的样式:
Style="{StaticResource MyStyle}"
在我的 ResourceDictionary
中有一个样式如下所示:
<Style x:Key="MyStyle" TargetType="{x:Type UserControl}">
<Style.Resources>
<Style TargetType="Label">
<!-- Label Setters -->
</Style>
<Style TargetType="TextBox">
<!-- TextBox Setters -->
</Style>
</Style.Resources>
</Style>
但是有没有一种方法可以直接在 ResourceDictionary
中设置 UserControl
的样式,例如:
<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
基本上我的问题是,我可以将样式直接应用到控件而不是控件组件吗?
编辑: 我想要完成的是如下内容:
<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
<Setter Property="Background" Value="Black"/>
</Style>
<Style x:Key="{x:Type MyControl}" TargetType="{x:Type MyControl}" BasedOn="{StaticResource MyStyle}"/>
第二行将样式应用于应用程序中的所有控件,如果您对普通控件执行类似的操作,则此方法有效。
然而,这仅设置了 UserControl
的 Background
,因此我如何将相同的背景应用到其组件。
如何使用 UserControl
?
要设置所有控件的样式,请将您的 ResourceDictionary 添加到 App.xaml 的资源中。
<Application.Resources>
<!-- Your Resources for the whole application here -->
</Application.Resources>
如果您使用应用程序打开主窗口...
<Application ...
MainWindow="MainWindow">
或在启动事件期间...
<Application ...
MainWindow="MainWindow">
Startup="Application_Startup">
您的 MainWindow 的每个控件都可以使用这些资源。
要为特定用户控件设置样式,请看这里: Set Style for user control
您可以像这样直接设置用户控件的样式:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style>
<Setter Property="local:MyControl.MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
或者像这样:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
用户控件资源中的默认样式也应该有效:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Resources>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Resources>
</UserControl>
您需要从定义的样式中删除 x:Key
,以便它可以普遍应用于与 TargetType
.
从 MSDN 引用 Style.TargetType Property:
Setting the TargetType property to the TextBlock type without setting an x:Key implicitly sets the x:Key to {x:Type TextBlock}. This also means that if you give the [...] Style an x:Key value of anything other than {x:Type TextBlock}, the Style would not be applied to all TextBlock elements automatically. Instead, you need to apply the style to the TextBlock elements explicitly.
在您的用户控件中 xaml 将样式放在资源标签内:
<UserControl>
<UserControl.Resources>
<Style ...</Style>
</UserControl.Resources>
//.. my other components
</UserControl>
特殊情况的死灵答案。如果用户控件是通过另一个 WPF 控件或 window 中的 DataTemplate 资源选择的,WPF 可能不会自动应用导入资源字典中的默认样式。但是,您可以在导入资源字典后应用命名样式资源。
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../../Resources/ResourceDictionary.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
<Binding Source="{StaticResource MyUserControlStyle}"></Binding>
</UserControl.Style>