WPF Override Metadata Foreground Dependency Property

advertisements

I'm hoping someone can shed some light on my problem as I've searched everywhere and can't find an explanation or solution to this.

To explain the problem I have created a class called Label which inherits from TextBlock and I want to override the default Foreground brush, Font Weight, Font Size and Font Family; all the overrides work apart from the Foreground (I can override the background without a problem so it is something specific to the foreground property).

Create a new WPF application and create this simple class: -

namespace WpfApplication
{
    public class Label : TextBlock
    {
        static Label()
        {
            ForegroundProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(Brushes.Red));
            FontWeightProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(FontWeights.Bold));
            FontSizeProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(50.0));
            FontFamilyProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(new FontFamily("Calibri")));
        }
    }
}

Create an instance of this new Label class in MainWindow.xaml: -

<Grid>
    <local:Label Text="TEST" />
</Grid>

You should see that the Label doesn't adopt the new Foreground default but looking in SNOOP it is still inheriting it's value.

As far as I'm aware any Dependency Property can be overridden so any help would be much appreciated!


One workaround is:

public class Label : TextBlock
    {
        static Label()
        {
            ForegroundProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(Brushes.Red, OnForegroundChanged));
            FontWeightProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(FontWeights.Bold));
            FontSizeProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(50.0));
            FontFamilyProperty.OverrideMetadata(typeof(Label), new FrameworkPropertyMetadata(new FontFamily("Calibri")));
        }

        private static void OnForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {

        MethodInfo mi = typeof(DependencyPropertyChangedEventArgs).GetMethod("get_OperationType",
                                                                              BindingFlags.NonPublic |
                                                                              BindingFlags.Instance);
        var v = mi.Invoke(e, null);

        if ((e.NewValue != Brushes.Red) && (v.ToString() == "Inherit"))
        {
            ((Label)d).Foreground = Brushes.Red;
        }
        else
        {
            ((Label)d).Foreground = (Brush)e.NewValue;
        }
    }
}

On VisualStudio 2012 in the design view the preview of you code show the text in red color. Something change it at runtime. But at the moment I can't tell you who and why... The dafault inherited value prevales!

Edit: here some good info.