WPF 文本框换行
我试图弄清楚如何让文本框来换行其内容,但是情况与典型的“它不换行”场景不太一样。我的文本框包含在 DataTemplate 中,该 DataTemplate 在 Telerik RadTabControl 实例中使用(使用 ContentTemplatePresenter 来确定要显示哪个视图),并且 DataTemplate 的 XAML 如下所示:
<DataTemplate x:Key="NotesTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Use the box below to record any general notes associated with this item." Style="{StaticResource Default}" />
<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" GridRow="1" Margin="20" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
我说它不属于正常“它”的原因不换行”场景是它用来换行,直到我不得不将视图更改为可调整大小以支持应用程序运行的不同屏幕尺寸。当我这样做时,文本框停止换行,因为(大概)当用户键入某些内容时,文本框会说“我需要更多空间”,因此父级有义务,并且该框无限期地继续向右延伸(尽管视图有滚动条)。我尝试使用 Binding/RelativeSource 设置 MaxWidth,但由于父级是专门为增长而设计的,因此该方法不起作用。我需要发生的是盒子应该是其包含父母的VisibleWidth的宽度。这意味着,如果窗口本身为 1024x768,则文本框的最大宽度应为 1024,此后的任何文本都会自动换行,但如果窗口增长到 1280x1024,则文本框现在应为 1280,文本也会相应地换行。我用这个绑定表达式尝试了这个场景,但没有运气:
MaxWidth="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=ActualWidth}"
窗口大小本身没有增长,所以如果我可以获得窗口的宽度(减去一定量以覆盖属于 TabControl 的选项卡的宽度),我相信那会起作用的。
有什么想法吗?
I am trying to figure out how to get a textbox to wrap its contents, however the situation isn't quite the same as the typical "it doesn't wrap" scenario. My textbox is contained inside a DataTemplate which is used inside a Telerik RadTabControl instance (using a ContentTemplatePresenter to determine which view to display) and the XAML for the DataTemplate looks like this:
<DataTemplate x:Key="NotesTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Text="Use the box below to record any general notes associated with this item." Style="{StaticResource Default}" />
<TextBox TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" GridRow="1" Margin="20" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" />
</Grid>
</DataTemplate>
The reason I say it doesn't fall under the normal "it doesn't wrap" scenario is it used to wrap until I had to change the view to be resizable to anything to support the varying screen sizes the app will be run on. When I did that the TextBox stopped wrapping because (presumably) as the user types something the TextBox says "I need more space" so the parent obliges and the box continues out to the right indefinitely (although the view gets scrollbars). I tried setting a MaxWidth using Binding/RelativeSource, but since the parent is specifically designed to grow that approach won't work. What I need to have happen is the box should be the width of its' containing parents' VisibleWidth. Meaning, if the Window itself is 1024x768, the TextBox's MaxWidth should be 1024 and any text thereafter would automatically wrap, but if the Window grows to 1280x1024 the box should now be 1280 and the text wrap accordingly. I tried this scenario with this binding expression, but no luck:
MaxWidth="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=ActualWidth}"
The Window size itself isn't growing so if I could get the Window's Width (minus a certain amount to cover the width of the tabs that are part of the TabControl) I believe that would work.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
禁用水平滚动视图,所以它将被强制换行。您可以尝试在
TextBox
本身或包装Grid
上禁用它。Disable the horizontal scrollView, so it will be forced to wrap. You can try to disable it on the
TextBox
itself, or on the wrappingGrid
.