wpfwpf-stylegeneric.xaml

Extending an existing Generic.xaml style from another WPF library


I am working with the HandyControls WPF library, but I want to adjust one of its default styles. I want to add an 'OnClick' event to all NumericUpDown controls, using a Style which applies an EventSetter. I want this style to apply to all NumericUpDowns in my application, just as HandyControls' own NumericUpDownBaseStyle does.

I have a Generic.xaml file. I know it works, because I already have a style in here for one of our custom UserControls (one that the HandyControls library does not have its own style for), and that style gets applied by default to all instances of that custom control.

But when I add the following to Generic.xaml:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="https://handyorg.github.io/handycontrol"
x:Class="Generic">

<Style BasedOn="{hc:StaticResource NumericUpDownBaseStyle}" TargetType="hc:NumericUpDown">
    <EventSetter Event="MouseDown" Handler="OnClick"/>
    <Setter Property="Background" Value="Red"/>
</Style>

the style is not applied to instances of NumericUpDown. Instead, NumericUpDown controls still use the default style from HandyControls.

If I place this style into a global resource dictionary (a dictionary which, since discovering Generic.xaml, I am trying to phase out), the style is applied, and I can see my NumericUpDowns turn red.

Why is my Generic.xaml file unable to take precedence over the styles defined in HandyControls? How can I fix this?


Solution

  • I posted my own answer to this a few days ago but that answer has been deleted? Great.

    In the end I solved this by merging the external library into the dictionary at the top of Generic.xaml. I still have no idea why I had to do this.

    <ResourceDictionary
        xmlns:hc="https://handyorg.github.io/handycontrol">
    
        <ResourceDictionary.MergedDictionaries>
            <!-- External Libraries -->
            <ResourceDictionary Source="/HandyControl;component/Themes/Theme.xaml"/>
        </ResourceDictionary.MergedDictionaries>>
    </ResourceDictionary>