0

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

4

1 回答 1

0

这是我使用 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);
}
于 2012-12-10T21:00:57.177 回答