0

メソッドでテキストを描画するだけの独自のカスタム WPF コントロールを作成しましたOnRenderが、デザイナーでこのコントロールを見ると、スペースを占有しません。所有しているオブジェクトによって報告されたように、コントロールが確実にスペースを占めるようにするにはどうすればよいFormattedTextですか?

4

1 に答える 1

0

これは、render メソッドで行っていることの例です。テキスト以外は本当に必要ありませんが、新しいジオメトリを作成した後、 InvalidateMeasureandの呼び出しがありませんでした。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 に答える