MinLines and MaxLines on TextBox not working

Can anyone explain why the next TextBox does not initially appear as 3 lines tall? It displays 1 line high, and then adjusts to 3 when I start typing.

Edit: here is some more 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>
+8
source share
3 answers

Yes, I also ran into this problem. The TextBox will not initially be configured correctly at startup, and you can workaround by linking the Text property to the startup text (say, the value of the Balkan space is "").

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>

WITH#:

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

private string startUpText;
public string StartUpText
{
  get { return startUpText; }
  set
  {
    if (value != startUpText)
    {
        startUpText = value;
    }
  }
}
+2
source

You can also use FallbackValue in the text:

<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, FallbackValue=whatyouwant}"
              AcceptsReturn="True"
              TextWrapping="Wrap" TextChanged="text_TextChanged" Loaded="text_Loaded"  />
     </DockPanel>
</StackPanel>
0
source

. :

"If the Height property is explicitly set in the TextBox, the values ​​of the MaxLines and MinLines properties are ignored." - https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.textbox.maxlines? view = NetFramework-4.8

0
source

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


All Articles