WPF TextBox Wrapping

时间:2011-10-19 21:55:21

标签: wpf textbox wrapping

我试图弄清楚如何让文本框包装其内容,但情况与典型的“它不包装”场景并不完全相同。我的文本框包含在一个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>

我之所以说它不属于正常的“它不包装”的情况是它用来包装,直到我不得不将视图更改为可调整大小以支持不同的屏幕尺寸应用程序将是运行。当我这样做时,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}"

窗口大小本身没有增长,所以如果我可以获得Window的宽度(减去一定量以覆盖TabControl中一部分的选项卡的宽度),我相信这样可行。

有什么想法吗?

1 个答案:

答案 0 :(得分:15)

  

虽然视图获取滚动条

禁用水平scrollView,因此它将被强制换行。您可以尝试在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>