How can I extend a TextBlock to display Outlined Text?
Asked Answered
H

1

0

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.

Herl answered 13/3, 2016 at 20:18 Comment(5)
Sounds like you want to use Effects. Your question is a little vague as strike and outline are two different operations. You want an outline around text or a line striking through?Jut
@SideriteZackwehdex Text Outline. Not strike through.Herl
@SideriteZackwehdex I only see two effects ( out of the box ) - Blur and Drop Shadow. I see no "Stroke" effect option.Herl
I think creating a new control is virtually inevitable, you're adding new DPs so they would have to be attached DPs, at which point your XAML is going to start looking a bit messy and won't play nicely with the IDE. Take a look at this page, it's still a new control but it inherits TextBlock and replaces its rendering.Burget
@MarkFeldman - I will take a look at this. I see they are using a TextBox though - I want to use a TextBLOCK - I'm just looking to display data, not facilitate data interaction.Herl
J
2

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.

Jut answered 13/3, 2016 at 22:10 Comment(2)
This is about right... Thanks. I'm still going to need to look deeper since i don't want to have to do this with EVERY textblock in my program. I suppose implementing a style could work but I was really hoping to be able to extend a TextBlock to get this done... Thanks.Herl
The effect can be set in a style that affects all Textblocks or only those that you set the style to.Jut

© 2022 - 2024 — McMap. All rights reserved.