c# - 如何在 TreeView 中包装 TextBlock 内容?

标签 c# wpf xaml treeview

我有TreeView ,它使用数据模板显示一些数据。这是 XAML:

<TreeView Grid.Row="0" ItemsSource="{Binding Railways}" x:Name="tvDatawareObjects"
          ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <!-- other templates here... -->
        <HierarchicalDataTemplate DataType="{x:Type viewModels:ProjectViewModel}" ItemsSource="{Binding Phases}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding Model.Code}" FontWeight="DemiBold" />
                <TextBlock Text="{Binding Model.Title}" TextWrapping="Wrap" Foreground="Gray" Grid.Row="1" />
            </Grid>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type viewModels:CollectionViewModel}" ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding CollectionName}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

<TextBlock Text="{Binding Model.Title}" TextWrapping="Wrap" Foreground="Gray" Grid.Row="1" /> 的文字换行不起作用。我做错了什么?

最佳答案

我认为 TextBlock 没有换行,因为它没有定义宽度。 TextBlock 所在的网格列的宽度会随着 TextBlock 宽度的增加而增加。尝试在 TextBlock 或列上设置宽度,看看更改是否会导致 TextBlock 换行。

更新:

更具体地说,问题是 TreeViewItem 将根据其内容的大小自行调整大小,ColumnDefinition 将填充(无限)可用空间,并且TextBlock,没有宽度限制,永远不会换行。 This post很好地描述了 TreeViewItem 的行为方式。总结一下:TreeViewItem 的内容区域设置为“自动”,因此它会增长以适应内容。要明确设置 TreeViewItem 的宽度,请尝试将您的 ColumnDefinition 宽度绑定(bind)到 TreeViewActualWidth

XAML:

<TreeView Width="100">
    <TreeViewItem>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="{Binding RelativeSource={RelativeSource AncestorType=TreeView}, Path=ActualWidth}"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <TextBlock Text="Lorem Ipsum" />
            <TextBlock Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book." 
                       TextWrapping="Wrap" Grid.Row="1"/>
            </Grid>
        </TreeViewItem>
    </TreeView>

关于c# - 如何在 TreeView 中包装 TextBlock 内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11209515/

相关文章:

c# - Automapper 无法翻译通用列表

c# - 阅读每行 2 多行文本框

c# - 如何在 DynamoDB 中的扫描中选择嵌套属性?

c# - 如何从Style.Triggers升级到Interaction.Behaviors?

c# - 增量 Nuget 包版本 NetStandard 本地文件夹 visual studio 2017

c# - 从 If 语句返回 Int

c# - 如何将 WPF 中的列表框绑定(bind)到通用列表?

c# - 如何覆盖 DataGridRow 模板中的 AlternatingRowBackground?

wpf - DataGridCheckBoxColumn IsReadOnly 属性绑定(bind)

c# - 如何将 MainWindow.xaml 拆分为单独的文件? - 外包<Style>?