Reputation: 157
The Handycontrols WPF library provides a control called NumericUpDown, which is a number field with a button for increasing and decreasing the value.
I'm making a control which derives from this control, called TuggableUpDown. I want the user to be able to click and drag on the arrows to scroll the value up and down, similar to software like Unity 3D.
I've created the derived class:
public class TuggableUpDown : NumericUpDown
{
}
Now I want to make a style for this class. Because the style does everything the style for NumericUpDown should do, I want to base it on that style. And because I want this style to be the default, I define it in 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"
xmlns:local="clr-namespace:MyLibrary"
xmlns:hx="clr-namespace:MyLibrary.HandyExtensions"
xmlns:interactivity="clr-namespace:HandyControl.Interactivity;assembly=HandyControl">
<Style TargetType="{x:Type local:TuggableUpDown}" BasedOn="{hc:StaticResource NumericUpDownBaseStyle}">
<Setter Property="Background" Value="Blue"/>
</Style>
But this throws an error when I load the control.
System.Windows.Markup.XamlParseException: ''Provide value on 'HandyControl.Themes.StaticResourceExtension' threw an exception.' Line number '10' and line position '52'.' Inner Exception Exception: Cannot find resource named 'NumericUpDownBaseStyle'. Resource names are case sensitive.
I'm using the hc: namespace elsewhere in this same resource dictionary (though not as a 'BasedOn' parameter). NumericUpDownBaseStyle is indeed the key of the style I'm trying to use, and is even shown by autocomplete. Why can't it be resolved?
Upvotes: 0
Views: 610
Reputation: 157
The solution seems to be to merge Handycontrols' theme dictionary into my generic.xaml dictionary.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/HandyControl;component/Themes/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type local:TuggableUpDown}" BasedOn="{hc:StaticResource NumericUpDownBaseStyle}">
<Setter Property="Background" Value="Blue"/>
</Style>
I still have no idea why this is needed, especially as I reference the hc: namespace just below this control in another template and it hasn't caused an issue so far. This also isn't how I thought namespaces worked: why would I need to merge content into a dictionary as well as importing a namespace in xaml? I can't find anything online that explains this bizarre behaviour.
Upvotes: 0