WPF 文本框包装

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7828691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:08:33  来源:igfitidea点击:

WPF TextBox Wrapping

wpftextboxword-wrap

提问by RubyHaus

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 中,该 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>

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:

我说它不属于正常的“它不包装”场景的原因是它用于包装,直到我不得不将视图更改为可调整大小以支持应用程序运行的不同屏幕尺寸。当我这样做时,TextBox 停止换行,因为(大概)当用户键入一些内容时,TextBox 说“我需要更多空间”,因此父级有义务并且该框无限期地向右继续(尽管视图获得滚动条)。我尝试使用 Binding/RelativeSource 设置 MaxWidth,但由于父级是专门为增长而设计的,因此该方法不起作用。我需要发生的是盒子应该是它的宽度,包含父母的VisibleWidth. 意思是,如果 Window 本身是 1024x768,则 TextBox 的 MaxWidth 应该是 1024 并且此后的任何文本都会自动换行,但是如果 Window 增长到 1280x1024,则该框现在应该是 1280 并且文本相应地换行。我用这个绑定表达式尝试了这个场景,但没有运气:

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.

窗口大小本身并没有增长,所以如果我能得到窗口的宽度(减去一定量来覆盖作为 TabControl 一部分的选项卡的宽度),我相信这会起作用。

Any ideas?

有任何想法吗?

回答by MichaelS

although the view gets scrollbars

虽然视图获得滚动条

Disable the horizontal scrollView, so it will be forced to wrap. You can try to disable it on the TextBoxitself, or on the wrapping Grid.

禁用水平滚动视图,所以它会被强制换行。您可以尝试在TextBox自身或包装上禁用它Grid

<DataTemplate x:Key="NotesTemplate">
    <Grid ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <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" Grid.Row="1" Margin="20" Text="{Binding Notes, UpdateSourceTrigger=PropertyChanged}" />
     </Grid>
</DataTemplate>