TextBox 与周围的 Grid 一起扩展,但不与文本一起扩展 [英] TextBox expanding with surrounding Grid but not with text
问题描述
一个窗口有一个包含两列的网格.左列包含一个具有恒定宽度但具有自适应高度的控件.右列包含一个 TextBox,它占用了 Grid 中的所有剩余空间(从而占用了 Window 中的空间).
A window has a Grid with two columns. The left column contains a control with a constant width but with a height that adapts. The right column contains a TextBox that takes up all remaining space in the Grid (and thereby in the Window).
Grid 具有最小的宽度和高度,并被包裹在 ScrollViewer 中.如果用户将窗口大小调整为小于 Grid 的最小宽度/高度,则会显示滚动条.
The Grid is given a minimal width and height and is wrapped within a ScrollViewer. If the user resizes the window to be smaller than the minimal width/height of the Grid, scrollbars are displayed.
这正是我想要的样子.但是,当用户开始输入文本时会出现问题.如果文本太长而无法放入 TextBox 中的一行,我希望文本换行.因此,我在 TextBox 上设置了 TextWrapping="Wrap"
.但是由于 TextBox 具有自动宽度并且被包裹在一个 ScrollViewer 中(它实际上是被包裹的整个 Grid),所以 TextBox 只是不断地向右扩展.
This is exactly how I want it to be. However, a problem occurs when the user starts typing text. If the text is to long to fit in one line in the TextBox, I want the text to wrap. Therefore I set TextWrapping="Wrap"
on the TextBox. But since the TextBox has an automatic width and is wrapped in a ScrollViewer (its actually the whole Grid that is wrapped), the TextBox just keeps expanding to the right.
如果窗口展开,我确实希望 TextBox 展开,但我不希望 TextBox 按文本展开.相反,文本应该包含在可用的 TextBox 内.如果文本不适合 TextBox 的高度,则应在 TextBox 内显示滚动条.
I do want the TextBox to expand if the window is expanded, but I don't want the TextBox to expand by the text. Rather the text should wrap inside the available TextBox. If the text don't fit within the TextBox height, a scrollbar should be displayed within the TextBox.
有没有办法做到这一点?
Is there a way to accomplish this?
下面是一些显示我的问题的代码:
Below is some code that shows my problem:
<Window x:Class="AdaptingTextBoxes.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400" Background="DarkCyan">
<Grid Margin="10" Name="LayoutRoot">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid MinWidth="300" MinHeight="200">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Margin="0,0,10,0" Content="Button" Width="100" />
<TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" />
</Grid>
</ScrollViewer>
</Grid>
</Window>
推荐答案
你可以使用一个不可见的边框(它很老套,但它可以工作 - 我倾向于在 Xaml 中整理动态文本框大小):
You could use an invisible border (its hacky but it works - its how I tend to sort out dynamic textbox sizes in Xaml):
<Border BorderThickness="0" x:Name="border" Grid.Column="1" Margin="0.5" />
<TextBox Grid.Column="1" AcceptsReturn="True" TextWrapping="Wrap" Width="{Binding ActualWidth, ElementName=border}" Height="{Binding ActualHeight, ElementName=border}" />
这篇关于TextBox 与周围的 Grid 一起扩展,但不与文本一起扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!