#928 – TextBox.MinLines Doesn’t Size Properly on Startup

When you give the MinLines property of a TextBox some value, you’ll find that the TextBox’s size isn’t adjusted to fit the minimum number of lines until after the user enters some text into the TextBox.  The TextBox will not initially be sized properly at startup.

You can work around this by doing the following:

  • Bind the Text property to some string-based property
  • At startup, set the value of the bound property to some text (e.g. a single space)
        <TextBox HorizontalAlignment="Stretch"
                 Margin="5"
                 Text="{Binding SomeText}"
                 MaxLines="3" MinLines="3"
                 VerticalScrollBarVisibility="Auto"/>

 

        public MainWindow()
        {
            this.InitializeComponent();
            this.DataContext = this;
            SomeText = " ";
        }

        private string someText;
        public string SomeText
        {
            get { return someText; }
            set
            {
                if (value != someText)
                {
                    someText = value;
                    RaisePropertyChanged("SomeText");
                }
            }
        }

928-001
You could also just manually set the TextBox to the desired size using the Height or MinHeight property.

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #928 – TextBox.MinLines Doesn’t Size Properly on Startup

  1. Pingback: Dew Drop – October 15, 2013 (#1,645) | Morning Dew

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: