我创建了一个继承自Combobox类的自定义组合框控件。在ResourceDictionary上为ComboxBox类型定义了一种类型样式,它自动应用于所有组合框,但这并未应用于自定义控件。
在App.xaml上定义的样式
<Application.Resources>
<Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
<Setter Property="Background" Value="Gray"/>
</Style>
</Application.Resources>
自定义组合框
public class AutoComboBox : ComboBox
MainWindow.xaml
<StackPanel Orientation="Vertical">
<ComboBox Width="200"/>
<local:AutoComboBox Width="200"/>
</StackPanel>
第一个组合框将背景显示为灰色,因为应用了App.xaml上定义的样式。但第二个组合框仍然显示默认的背景颜色。
我们是否可以在自定义控件上应用相同的类型样式而不为自定义控件创建重复样式?
答案 0 :(得分:0)
您使用的是主题方法吗?
如果是这样,你应该有../Themes/Generic.xaml
这是你的ResourceDictionary。
您的ResourceDictionary中应该有一个<Style>
,并为您的样式设置TargetType="{x:Type local:YourControl}"
。
此外,您应该在AssemblyInfo.cs文件中检查是否为每个主题设置了ResourceDictionaryLocation.SourceAssembly
。