سؤال

We're writing a custom subclass of TextBox where we need to change only some basic parts of the style. Now we know when you subclass Control, normally you're supposed to change the metadata like this...

public class EnhancedTextBox : TextBox
{
    static EnhancedTextBox()
    {
        // Commenting this line out lets this use the default TextBox style.
        DefaultStyleKeyProperty.OverrideMetadata(
            typeof(EnhancedTextBox),
            new FrameworkPropertyMetadata(typeof(EnhancedTextBox)));
    }
}

This changes the key used for that subclass type to be the type itself, which means it no longer gets the default TextBox style. No biggie... we just comment that line out and it again works since the key is still set to the value used by TextBox directly.

However, we're wondering if we did want to change a few things in the style, but not the entire style, we could simply create a new style and set its BasedOn property... but what do we set there? We don't want to have to pull out the XAML manually and create a new style, give it a key, then use a StaticResource, but we haven't found out what we could set there to say 'This is based on the TextBox style.

I'm hoping its something simple but again, we haven't found it. Can anyone help?

هل كانت مفيدة؟

المحلول

And just like that... I found it. Man, kicking myself that it was this obvious and I missed it!

<Style TargetType="{x:Type local:EnhancedTextBox}"
    BasedOn="{StaticResource {x:Type TextBox}}">

   ....

</Style>

Found it here... WPF style basedon current

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top