Will
Will

Reputation: 3585

How can I extend a TextBlock to display Outlined Text?

My question relates to this question.

This question is quite dated, and the answer more so and it feels like it is lacking somewhat. The user created a control from scratch that is lacking somewhat, but the biggest point is that it doesn't feel like it should be necessary ( to me, at least ) to create an entirely new control in order to have a TextBlock that can support stroked ( outlined ) text.

I'm trying to extend the TextBlock control by providing a Brush property and a double property. I had hoped I could override the OnRender method but it's sealed, so I can't.

This is what I have so far ( it's not much ) :

public class StrokedText: TextBlock {
    public static readonly DependencyProperty
        StrokeProperty = DependencyProperty.Register(
            "Stroke",
            typeof( Brush ),
            typeof( StrokedText ),
            new PropertyMetadata(
                Brushes.Red,
                ( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) ),
        StrokeWidthProperty = DependencyProperty.Register(
            "StrokeWidth",
            typeof( double ),
            typeof( StrokedText ),
            new PropertyMetadata(
                2.0D,
                ( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) );

    /// <summary>
    /// Get or Set Stroke Brush.
    /// </summary>
    public Brush Stroke {
        get { return this.GetValue( StrokeProperty ) as Brush; }
        set { this.SetValue( StrokeProperty, value ); }
    }

    /// <summary>
    /// Get or Set Stroke Width
    /// </summary>
    public double StrokeWidth {
        get { return ( double )this.GetValue( StrokeWidthProperty ); }
        set { this.SetValue( StrokeWidthProperty, value ); }
    }

I've been spending some time looking at the TextBlock control in order to trace where it actually renders the string as text on the screen ( I figured if I could find that I could copy the method ), but I was hoping someone might happen to already know the answer and save me some time as the TextBlock control is just... insane.

So - is it possible for me to extend this TextBlock so that I can stroke the text like I want?


EDIT 1 :

For clarity, there seems to have been a misunderstanding as to for what I am going - I do not care about outlining the text block. I want to outline the TEXT itself.

Upvotes: 0

Views: 3456

Answers (1)

Siderite Zackwehdex
Siderite Zackwehdex

Reputation: 6570

Something like this:

<TextBlock >
    <TextBlock.Effect>
        <DropShadowEffect ShadowDepth="0"
                    Color="Red"
                    Opacity="1"
                    BlurRadius="5"/>
    </TextBlock.Effect>
    Some text that we want outlined
</TextBlock>

Using DropShadowEffect to simulate the Glow/Outline effect.

Upvotes: 2

Related Questions