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.

Community
  • 1
  • 1
Will
  • 3,413
  • 7
  • 50
  • 107
  • 1
    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? – Siderite Zackwehdex Mar 13 '16 at 21:37
  • @SideriteZackwehdex Text Outline. Not strike through. – Will Mar 13 '16 at 21:52
  • @SideriteZackwehdex I only see two effects ( out of the box ) - Blur and Drop Shadow. I see no "Stroke" effect option. – Will Mar 13 '16 at 22:00
  • 1
    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](https://dalebarnardonwpf.wordpress.com/2009/09/25/customizing-wpf-textbox/), it's still a new control but it inherits TextBlock and replaces its rendering. – Mark Feldman Mar 13 '16 at 22:08
  • @MarkFeldman - I will take a look at this. I see they are using a TextBox though - I want to use a Text***BLOCK*** - I'm just looking to display data, not facilitate data interaction. – Will Mar 13 '16 at 22:10

1 Answers1

1

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.

Siderite Zackwehdex
  • 6,293
  • 3
  • 30
  • 46
  • 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. – Will Mar 13 '16 at 22:13
  • The effect can be set in a style that affects all Textblocks or only those that you set the style to. – Siderite Zackwehdex Mar 15 '16 at 15:04