wpfxamltextbox

MinLines and MaxLines on TextBox not working


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>

Solution

  • 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;
        }
      }
    }