【发布时间】:2016-07-05 02:03:34
【问题描述】:
我知道我可以通过添加属性在控件中设置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?
【问题讨论】:
标签: c# wpf user-controls