Thomas Slade
Thomas Slade

Reputation: 157

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?

Upvotes: -1

Views: 124

Answers (2)

Thomas Slade
Thomas Slade

Reputation: 157

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>

Upvotes: -1

mm8
mm8

Reputation: 169370

Generic.xaml is used to define the default style for a control. It's not used to extend or override a default style for a control that is defined in another assembly.

You should use a "global" style to do this, i.e. put your custom style that is based on the NumericUpDownBaseStyle directly in App.xaml or in a resource dictionary that is merged from App.xaml. Do not put it in Generic.xaml.

Upvotes: 0

Related Questions