I'm attempting to extend a Label control to support stroking the text ( sort of like this questions answer, but I want to extend a Label to do it rather than write a brand new ( ish ) control ).
I've had a measure of success but I hit a wall in which I can't find how the Label renders text. If I could figure out how that happens ( and override it ) then I could add a few properties to my extended Label class ( a brush and a double ) and be all set.
This is what I have so far :
public class StrokedLabel : Label {
public static readonly DependencyProperty
StrokeProperty = DependencyProperty.Register(
"Stroke",
typeof( Brush ),
typeof( StrokedLabel ),
new PropertyMetadata(
Brushes.Red,
( S, E ) => ( S as StrokedLabel ).InvalidateVisual( ) ) ),
StrokeWidthProperty = DependencyProperty.Register(
"StrokeWidth",
typeof( double ),
typeof( StrokedLabel ),
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 ); }
}
protected override void OnRender( DrawingContext drawingContext ) {
if ( !( this.Content is string ) )
base.OnRender( drawingContext );
else {
drawingContext.DrawGeometry(
this.Foreground,
new Pen(
this.Stroke,
this.StrokeWidth ),
new FormattedText(
this.Content.ToString( ),
CultureInfo.CurrentUICulture,
this.FlowDirection,
new Typeface(
this.FontFamily,
this.FontStyle,
this.FontWeight,
this.FontStretch ),
this.FontSize,
this.Foreground ).BuildGeometry( new Point( 0.0D, 0.0D ) ) );
}
}
}
I've tried tracing the chain of inheritance and I've made it as far as UIElement
which defines the OnRender
method, but I can't find where in the chain of inheritance that method is actually used ( or if it's even involved ).
I feel like the text is rendered within the ContentControl ( from which the Label directly inherits ) but I can't find where in that control the text is rendered. Is it rendered in the Content Control, or is it rendered in what the Content Control inherits ( the Control class )?
In which class ( and through which method(s) ) is a string rendered as text?