0
votes

I have created a custom combo box control which inherits from Combobox class. There is a type style defined for ComboxBox type on ResourceDictionary which applied automatically for all combo boxes but this is not getting applied the custom control.

Style defined on App.xaml

<Application.Resources>
    <Style x:Key="{x:Type ComboBox}" TargetType="{x:Type ComboBox}">
        <Setter Property="Background" Value="Gray"/>
    </Style>
</Application.Resources>

Custom ComboBox

public class AutoComboBox : ComboBox

MainWindow.xaml

<StackPanel Orientation="Vertical">
        <ComboBox Width="200"/>
        <local:AutoComboBox Width="200"/>
</StackPanel>

The first combo box will have Background as Gray color as the style defined on App.xaml is applied. But the second combo box still show the default background color.

Is there any way we can apply the same type style on custom control without creating duplicate style for custom control?

1
Show your code: XAML + Custom Control.Anatoliy Nikolaev
Inheritance to extend WPF controls is discouraged - why not have a global Style set the Template on evey ComboBox - or if you want to apply it selectively give the Style a key.markmnl
I am using inheritance to provide auto complete feature on combo box using Rx framework which I can not achieve by style.pchajer
Will the style be applied if you change the type from ComboBox to AutoComboBox? Just for testing.Mighty Badaboom

1 Answers

0
votes

Are you using a Theme approach?

If so you should have a ../Themes/Generic.xaml which is your ResourceDictionary.

Your ResourceDictionary should have a <Style> in it with the TargetType="{x:Type local:YourControl}" set for your style.

Furthermore, you should check that in your AssemblyInfo.cs file that ResourceDictionaryLocation.SourceAssembly is set for each of your themes.