如何在 WPF TreeViewItem 中使文本换行?

发布于 2024-12-27 17:57:53 字数 1593 浏览 0 评论 0原文

这次,我的问题听起来很简单...如何让文本换行到 WPF TreeViewItem 中?

我有一个简单的 HierarchicalDataTemplate,其中只有一个 TextBlock

<TextBlock Text="{Binding Value}" TextWrapping="Wrap" />

文本不换行。

我尝试将 TextBlockWidth 绑定到 TreeViewActualWidth,但是尽管这会使文本换行,TreeViewItemWidth 不适合 TreeView,因为 TreeViewPadding.绑定到 TreeViewItemActualWidth 具有(毫不奇怪)相同的效果。这样做的另一个缺点是,即使是只有很少文本的项目也会延伸到 TreeView 边界之外。

<TextBlock Text="{Binding Value}" TextWrapping="Wrap" Width="{Binding ActualWidth, 
ElementName=TreeView}" />

当然一定有更好的方法,比如以某种方式通知 TreeViewItem TreeView 的边界...我不敢相信它不会自动知道。但我该怎么做呢?

UPDATE >>>

感谢 HB 的回答,我设法将 Bd Border 上的 Grid.ColumnSpan 更改为 2 他在 ControlTemplate 中提到,它设置了宽度,以便文本现在可以很好地换行。问题是我将此 ControlTemplate 用于其他我不想要全宽项目的 TreeView 中的其他 TreeViewItem

我想出的解决方案很简单。我已将 TreeViewItem.Tag 值绑定到 ControlTemplate 中的 Grid.ColumnSpan 属性,如下所示:

<Border Grid.ColumnSpan="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" 
Name="Bd" Grid.Column="1" ... />

这允许我更改 通过将 TreeViewItem.Tag 值分别设置为 2 或 1,可以实现 Grid.ColumnSpan 以及 TreeViewItem 的全宽或普通宽度行为。

This time, my question is as simple as it sounds... how do you get text to wrap in a WPF TreeViewItem?

I have a simple HierarchicalDataTemplate with just one TextBlock in it.

<TextBlock Text="{Binding Value}" TextWrapping="Wrap" />

The text does not wrap.

I tried binding the Width of the TextBlock to the ActualWidth of the TreeView, but although that makes the text wrap, the Width of the TreeViewItem does not fit in the TreeView because the TreeView has Padding. Binding to the ActualWidth of the TreeViewItem has (unsurprisingly) the same effect. Another downside to this is that even the items with little text stretch outside the TreeView bounds.

<TextBlock Text="{Binding Value}" TextWrapping="Wrap" Width="{Binding ActualWidth, 
ElementName=TreeView}" />

Surely there must be a better way, like somehow informing the TreeViewItem of the TreeView's bounds... I can't believe it doesn't know automatically. But how can I do this?

UPDATE >>>

Thanks to H.B.'s answer, I managed to change the Grid.ColumnSpan to 2 on the Bd Border he mentioned in the ControlTemplate and it set the width so that the text now wraps nicely. The problem is that I am using this ControlTemplate for other TreeViewItems in other TreeViews where I don't want full width items.

The solution I came up with is simple. I have bound the TreeViewItem.Tag value to the Grid.ColumnSpan property in the ControlTemplate like so:

<Border Grid.ColumnSpan="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" 
Name="Bd" Grid.Column="1" ... />

This allows me to change the Grid.ColumnSpan and therefore the full width or ordinary width behaviour of the TreeViewItem by setting the TreeViewItem.Tag value to either 2 or 1 respectively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2025-01-03 17:57:53

如果您查看TreeViewItems默认模板 code> 您将看到这样的网格:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="19"
                          Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <!-- ... -->

正如您所看到的,第三列占用了所有可用空间,而其他列处于 Auto 状态,标题放置在边框内的第二列中:

<Border Name="Bd"
        Grid.Column="1"
        ...

这意味着该列将变得一样大作为标题,对其没有任何限制。因此,标题会变得比 TreeView 本身更大。

如果将 Grid.ColumnSpan="2" 添加到此边框,它也会占据第三列,该列受到剩余空间的限制,因此文本将换行;然而,这会将标题扩展到整个宽度,选择它时可能看起来有点奇怪。

当然,您还需要禁用水平滚动:

<TreeView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...

A Screenshot

If you look at the default template of TreeViewItems you will see a Grid like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition MinWidth="19"
                          Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <!-- ... -->

As you can see the third column takes all available space while the others are on Auto, the header is placed in the second column inside a border:

<Border Name="Bd"
        Grid.Column="1"
        ...

This means that the column will become as large as the header, there is no restriction on it. Thus the header just gets bigger than the TreeView itself.

If you add Grid.ColumnSpan="2" to this Border it will occupy the third column as well, which is restricted by how much space is left, hence the text will wrap; this will however extend the header across the whole width which might look a bit odd when selecting it.

Of course you will also need to disable horizontal scrolling:

<TreeView ScrollViewer.HorizontalScrollBarVisibility="Disabled" ...

A screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文