C# 确保WPF中自定义控件的边界正确

C# 确保WPF中自定义控件的边界正确,c#,wpf,C#,Wpf,我创建了自己的自定义WPF控件,该控件仅在其OnRender方法中绘制一些文本,但是,当我在设计器中查看该控件时,它不会占用任何空间。我需要做什么来确保我的控件占用我拥有的FormattedText对象所报告的空间?下面是我使用render方法所做的一个示例。我真的不需要任何东西,但是,在创建新几何体后,我错过了对InvalidateMeasure和InvalidateArrange的调用 private Geometry textGeometry; protected override vo

我创建了自己的自定义WPF控件,该控件仅在其
OnRender
方法中绘制一些文本,但是,当我在设计器中查看该控件时,它不会占用任何空间。我需要做什么来确保我的控件占用我拥有的
FormattedText
对象所报告的空间?

下面是我使用render方法所做的一个示例。我真的不需要任何东西,但是,在创建新几何体后,我错过了对
InvalidateMeasure
InvalidateArrange
的调用

private Geometry textGeometry;

protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
    if (textGeometry == null)
    {
        var currentTypeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);

        var formattedText = new FormattedText(Text
            , System.Globalization.CultureInfo.CurrentCulture
            , System.Windows.FlowDirection.LeftToRight
            , currentTypeface
            , FontSize
            , Foreground
            );

        var d = LineHeight - formattedText.Baseline;

        textGeometry = formattedText.BuildGeometry(new Point(-formattedText.OverhangLeading, d));
        textGeometry.Freeze();

        this.InvalidateMeasure();
        this.InvalidateArrange();
    }

    drawingContext.DrawGeometry(Foreground, null, textGeometry);
}

你的控件具体做什么?我不认为覆盖
OnRender()
对于几乎任何情况都是最好的方法。它只是绘制文本,目的只是绘制文本,而不带前导悬垂,并且在线条高度的基线处。我认为我没有正确理解。控件与常规的
TextBlock
之间有什么区别?