设置 WPF UserControl 的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36082315/
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
Setting the style of a WPF UserControl
提问by TheLethalCoder
I know I can set the style of a UserControllike so in the control by adding an attribute:
我知道我可以UserControl通过添加一个属性在控件中设置类似的样式:
Style="{StaticResource MyStyle}"
And having a style in my ResourceDictionarythat looks something like the following:
并且在我的样式中具有如下所示的样式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>
But is there a way I can set the style of the UserControlin the ResourceDictionarydirectly like:
但是有没有一种方法可以直接设置UserControlin 的样式,ResourceDictionary例如:
<Style x:Key="MyStyle" TargetType="{x:Type MyControl}">
Essentially my question is, can I apply the style directly to the control instead of to the controls components?
基本上我的问题是,我可以将样式直接应用于控件而不是控件组件吗?
EDIT:What I am trying to accomplish is something like the following:
编辑:我想要完成的事情如下:
<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}"/>
Where the second line applies the style to all controls in the application, if you do something similar with a normal control this approach works.
第二行将样式应用于应用程序中的所有控件,如果您对普通控件执行类似操作,则此方法有效。
However this only sets the Backgroundof the UserControl, so how can I apply that same background to its components.
然而,这只是设置Background的UserControl,我怎么能适用相同的背景及其组件。
How can I do it with the UserControl?
我该怎么做UserControl?
采纳答案by slugster
You need to remove the x:Keyfrom your defined style so that it can be applied universally to all controls of the same type as what is defined in the TargetType.
您需要x:Key从定义的样式中删除 ,以便它可以普遍应用于与TargetType.
To quote from MSDN for Style.TargetType Property:
从 MSDN 引用Style.TargetType 属性:
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.
将 TargetType 属性设置为 TextBlock 类型而不设置 x:Key 将 x:Key 隐式设置为 {x:Type TextBlock}。这也意味着如果你给 [...] 样式一个 x:Key 值而不是 {x:Type TextBlock},样式将不会自动应用于所有 TextBlock 元素。相反,您需要将样式显式应用于 TextBlock 元素。
回答by Clemens
You can directly set the UserControl's Style like this:
您可以像这样直接设置 UserControl 的样式:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style>
<Setter Property="local:MyControl.MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
or like this:
或者像这样:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Style>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Style>
</UserControl>
A default Style in the UserControl's Resources should also work:
UserControl 资源中的默认样式也应该有效:
<UserControl x:Class="MyNamespace.MyControl" xmlns:local="MyNamespace" ...>
<UserControl.Resources>
<Style TargetType="local:MyControl">
<Setter Property="MyProperty" Value="..."/>
...
</Style>
</UserControl.Resources>
</UserControl>
回答by JJ_Coder4Hire
in your user control xaml place the style inside the resources tag:
在您的用户控件 xaml 中,将样式放置在资源标签内:
<UserControl>
<UserControl.Resources>
<Style ...</Style>
</UserControl.Resources>
//.. my other components
</UserControl>
回答by Fruchtzwerg
To style all controls, add your ResourceDictionary to the resources of your App.xaml.
要设置所有控件的样式,请将您的 ResourceDictionary 添加到 App.xaml 的资源中。
<Application.Resources>
<!-- Your Resources for the whole application here -->
</Application.Resources>
If your open your Mainwindow with the App...
如果您使用应用程序打开主窗口...
<Application ...
MainWindow="MainWindow">
or during the startup event...
或在启动事件期间...
<Application ...
MainWindow="MainWindow">
Startup="Application_Startup">
the resources are available in every control of your MainWindow.
资源在 MainWindow 的每个控件中都可用。
To set the style for a specific usercontrol look here: Set Style for user control

