如何在 WPF TreeViewItem 中使文本换行?
这次,我的问题听起来很简单...如何让文本换行到 WPF TreeViewItem
中?
我有一个简单的 HierarchicalDataTemplate
,其中只有一个 TextBlock
。
<TextBlock Text="{Binding Value}" TextWrapping="Wrap" />
文本不换行。
我尝试将 TextBlock
的 Width
绑定到 TreeView
的 ActualWidth
,但是尽管这会使文本换行,TreeViewItem
的 Width
不适合 TreeView
,因为 TreeView
有 Padding.绑定到
TreeViewItem
的 ActualWidth
具有(毫不奇怪)相同的效果。这样做的另一个缺点是,即使是只有很少文本的项目也会延伸到 TreeView 边界之外。
<TextBlock Text="{Binding Value}" TextWrapping="Wrap" Width="{Binding ActualWidth,
ElementName=TreeView}" />
当然一定有更好的方法,比如以某种方式通知 TreeViewItem
TreeView
的边界...我不敢相信它不会自动知道。但我该怎么做呢?
UPDATE >>>
感谢 HB 的回答,我设法将 Bd
Border 上的
他在 Grid.ColumnSpan
更改为 2ControlTemplate
中提到,它设置了宽度,以便文本现在可以很好地换行。问题是我将此 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.ColumnSpanTreeViewItem
的全宽或普通宽度行为。
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 TreeViewItem
s in other TreeView
s 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您查看
TreeViewItems默认模板 code> 您将看到这样的网格:
正如您所看到的,第三列占用了所有可用空间,而其他列处于
Auto
状态,标题放置在边框内的第二列中:这意味着该列将变得一样大作为标题,对其没有任何限制。因此,标题会变得比 TreeView 本身更大。
如果将 Grid.ColumnSpan="2" 添加到此边框,它也会占据第三列,该列受到剩余空间的限制,因此文本将换行;然而,这会将标题扩展到整个宽度,选择它时可能看起来有点奇怪。
当然,您还需要禁用水平滚动:
If you look at the default template of
TreeViewItems
you will see a Grid like this: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: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: