WPF TextBox recalculates size

When using a wpf text field without explicit height and width values, and also when space is available for expansion, the text field changes size when you type.

However, when I change the thickness of the border, it does not recount it for very tight borders, part of the text is covered by the border. How to explicitly speed up recalc?

Most recently, I use a derived custom text field class, so I need to know when the border thickness changes.

enter image description here

+3
source share
2 answers

This error should be some optimization error.

  • Overriding metadata for BorderThicknessor adding a dependency property that affects Measure, Arrange, or Render doesn't help
  • , , , , ,

, Text, Width, Height . Reflector, , .

, BorderThickness , Width ,

public class MyTextBox : TextBox
{
    public MyTextBox()
    {
        DependencyPropertyDescriptor borderThickness
            = DependencyPropertyDescriptor.FromProperty(MyTextBox.BorderThicknessProperty, typeof(MyTextBox));
        borderThickness.AddValueChanged(this, OnBorderThicknessChanged);
    }
    void OnBorderThicknessChanged(object sender, EventArgs e)
    {
        double width = this.Width;
        SizeChangedEventHandler eventHandler = null;
        eventHandler = new SizeChangedEventHandler(delegate
        {
            this.Width = width;
            this.SizeChanged -= eventHandler;
        });
        this.SizeChanged += eventHandler;
        this.Width = this.ActualWidth + 0.00000001;
    }
}
+2

, .

, , , AffectsMeasure FrameworkPropertyMetadata . , .

, TextBox () .

+1

Source: https://habr.com/ru/post/1796539/


All Articles