WPF doesn't honor Textbox.MinLines for Auto height calculation

穿精又带淫゛_ 提交于 2019-12-24 05:06:21

问题


I want to have a TextBox which Height grows as Iam entering lines of Text.

I've set the Height property to "Auto", and so far the growing works. Now I want that the TextBox's Height should be at least 5 lines. Now I've set the MinLines property to "5" but if I start the app the TextBox's height is still one line.


回答1:


Try setting the MinHeight property.




回答2:


A hack to make the MinLines property work

public class TextBoxAdv : TextBox
{
    bool loaded = false;


    /// <summary>
    /// Constructor
    /// </summary>
    public TextBoxAdv()
    {
        Loaded += new RoutedEventHandler( Control_Loaded );

        SetResourceReference( StyleProperty, typeof( TextBox ) );
    }


    void Control_Loaded( object sender, RoutedEventArgs e )
    {
        if( !loaded )
        {
            loaded = true;

            string text = Text;
            Text = "Text";
            UpdateLayout();
            Text = text;
        }
    }
}


来源:https://stackoverflow.com/questions/830912/wpf-doesnt-honor-textbox-minlines-for-auto-height-calculation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!