开发者

Displaying a text box to show three lines

开发者 https://www.devze.com 2023-03-28 10:16 出处:网络
I would like to make a WPF text box which is big enough to show 3 lines of text. So far, I have this code:

I would like to make a WPF text box which is big enough to show 3 lines of text. So far, I have this code:

  System.Windows.Controls.TextBox myTextbox = new TextBox()
                {开发者_如何学C
                    AcceptsReturn = true,
                    MinLines = 3,
                    MaxLines = 3,
                    TextWrapping = TextWrapping.Wrap,
                    FontFamily = new FontFamily("Microsoft Sans Serif"),
                    FontSize = 11,
                };

However, when myTextBox is laid out there is no guarantee that it gets enough height to show 3 lines of text. Ideally, I would like to specify the FontSize to be "so small that myTextBox has room for 3 lines of text, and no smaller". Is there a way to do this?


Here's an illustration based on my comment above.

<Border VerticalAlignment="Top" Height="Auto">
  <TextBox FontSize="11" MinLines="3" MaxLines="3" AcceptsReturn="True" FontFamily="Microsoft Sans Serif" VerticalAlignment="Top"/>
</Border>

gives me three lines.

<Border VerticalAlignment="Top" Height="11">
  <TextBox FontSize="11" MinLines="3" MaxLines="3" AcceptsReturn="True" FontFamily="Microsoft Sans Serif" VerticalAlignment="Top"/>
</Border>

only gives me one. The Border is constraining the height of the TextBox.


Well, I ended up simply putting the TextBox into a ScrollViewer, like this:

System.Windows.Controls.ScrollViewer myScrollViewer = 
    new ScrollViewer(){Content = myTextbox};
// Now place the ScrollViewer where the TextBox was before.
0

精彩评论

暂无评论...
验证码 换一张
取 消