Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MinLines and MaxLines on TextBox not working

Tags:

wpf

textbox

xaml

Can anyone explain why the following TextBox doesn't initially display as 3 lines high? It displays 1 line high and then adjusts to 3 when I start typing text.

Edit: Here's some more of the form

<Window x:Class="MyNamespace.Views.DetailsView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:MV="clr-namespace:MyNamespace.Views"
    xmlns:prop="clr-namespace:MyNamespace.Properties"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    MV:DialogCloser.DialogResult="{Binding Path=DialogResult, Mode=TwoWay}"
    Background="{StaticResource {x:Static SystemColors.ControlBrushKey}}" 
    Title="{Binding Source={x:Static prop:Resources.MyView_Caption}}"
    SizeToContent="WidthAndHeight" 
    WindowStartupLocation="CenterScreen" 
    WindowStyle="SingleBorderWindow" 
    MinHeight="100" 
    MinWidth="250">
<StackPanel Name="AllItems" Orientation="Horizontal">
    <StackPanel Width="450" Margin="5">
        <StackPanel Margin="5,0,5,0" VerticalAlignment="Center">
            <DockPanel Margin="5" >
                <Label Content="Prompt"/>
                <TextBox MaxLines="3"
                     MinLines="3" 
                     VerticalScrollBarVisibility="Auto"
                     TextWrapping="Wrap" />
            </DockPanel>
        </StackPanel>
    </StackPanel>
</StackPanel>
...
</Window>
like image 328
Matt Fitzmaurice Avatar asked Mar 11 '14 09:03

Matt Fitzmaurice


1 Answers

Yes, i have too faced this problem. The TextBox will not initially be sized properly at startup and you can workaround this by binding the Text property with an startup text(Say a balnk space value " ").

XAML:

<StackPanel Margin="5,0,5,0" VerticalAlignment="Center">
 <DockPanel Margin="5" >
 <Label Content="Prompt"/>
 <TextBox MaxLines="3"
         MinLines="3" x:Name="text"
         VerticalScrollBarVisibility="Auto"
         Text="{Binding StartUpText}"
         AcceptsReturn="True"
         TextWrapping="Wrap" TextChanged="text_TextChanged" Loaded="text_Loaded"  />
 </DockPanel>
</StackPanel>

C#:

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

private string startUpText;
public string StartUpText
{
  get { return startUpText; }
  set
  {
    if (value != startUpText)
    {
        startUpText = value;
    }
  }
}
like image 54
Divya Avatar answered Sep 20 '22 11:09

Divya