Inheritance loves WPF

inheritance[1]  For the ToDo application, I’ve been adding in some new toys and causing bugs.  One feature I’m adding in is text glow around the listed text.  So lets talk about doing this programmatically.

When doing effects (I’ll talk about .Net 3.5 SP1 also here), you really need to embrace object oriented programming here.  Why?  WPF is pretty strong on the inheritance.  If you don’t know what inheritance is, hopefully you’ll gain an understanding.  I tend to think inheritance uses the phrase “is a ”.  So a Manager is an Employee.  A TextBox is a Control.  See?  Easy.  X inherits Y’s properties.

Lets say you have a TextBox and you have a Background.  Well, a background is Brush which can be multiple things.  A SolidColorBrush or an ImageBrush are just a few examples, MSDN has a full overview of brushes too.

Lets see an example using Background via XAML:

<TextBox>
    <TextBox.Background>
        <SolidColorBrush Color="Red" />
    </TextBox.Background>
</TextBox>

Lets see the same example using c#:

  txt.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(255,255,0,0))

See the difference?  More so see how I put a SolidColorBrush object into a Brush object?  That is due to polymorphism and inheritance.

Now lets create a WPF TextBox programmatically.

private static TextBox createTextBox()
{
    var txt = new TextBox
    {
        Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0,0,0,0)),
        Foreground = new SolidColorBrush(Settings.Default.FontColor),
        IsEnabled = true,
        AllowDrop = false,
        BorderThickness = new Thickness(0)
    };
            
    if (Settings.Default.GlowColor.A != 0)
        txt.BitmapEffect = new OuterGlowBitmapEffect { GlowColor = Settings.Default.GlowColor, GlowSize = 5 }; ;

    return txt;
}

So one issue I’ve found is with .Net 3.5 SP1.  They will deprecate BitmapEffects but they don’t have a replacement yet for the OuterGlowBitmapEffect.  In the future you should use Effect instead of BitmapEffect.

Another issue is the needle in the haystack issue.  Unless you know about predefined brushes or effects, it is a tad annoying finding them.  Since “Effect” and “Brush” are post-append, I would personally put them in a new namespace, however .Net has a legacy of doing it the current way.  It is just so new, I haven’t memorized everything there.  I think it would be great if I had all my brushes in System.Windows.Media.Brushes.  New discovery is super easy then, oh the perfect world.

No comments posted yet.

Post a Comment

Please add 3 and 7 and type the answer here: