TreeViewItem.Header 内有 Grid
我试图让“Img”出现在 TreeViewItem.Header 的末尾(靠近 TreeView 控件的右侧),但无论我尝试什么,标题宽度总是小于 TreeView 大小,当然“Img”出现在某处在控件的中间。 这可能是一个非常新的问题; 我刚刚开始学习WPF。
<TreeView Grid.Row="1" Grid.ColumnSpan="2" Margin="3,3,3,3" Name="treeView1" Width="300">
<TreeViewItem HorizontalAlignment="Stretch">
<TreeViewItem.Header>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0">General</Label>
<Label Grid.Column="1" Grid.Row="0">Img</Label>
</Grid>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
I'm trying to make "Img" appear in the end of TreeViewItem.Header (as close to the right side of TreeView control), but no mater what I try header wide is always less than TreeView size and ofcourse "Img" appear somewhere in the middle of the control. This probably a very newbish question; I'm just starting to learn WPF.
<TreeView Grid.Row="1" Grid.ColumnSpan="2" Margin="3,3,3,3" Name="treeView1" Width="300">
<TreeViewItem HorizontalAlignment="Stretch">
<TreeViewItem.Header>
<Grid HorizontalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="30" />
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0">General</Label>
<Label Grid.Column="1" Grid.Row="0">Img</Label>
</Grid>
</TreeViewItem.Header>
</TreeViewItem>
</TreeView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要实现此目的,您需要使用 TreeView 的 ItemContainerStyle 更改 TreeviewItem 的 Control 模板(这是应用于树视图根中任何项目的样式)。
默认的 TreeViewItem 不会拉伸,因此它不会一直向右延伸。
当您设置 Header 时,它位于 TreeViewItem 内部,因此无法超出它。
我不会发布整个样式,因为它太长了。
以下是在 Blend 中执行的操作:
选择您的 TreeViewItem,右键单击并选择“编辑控件部件/编辑副本”。 将样式保存在您想要的任何位置。
现在,在模板中,展开内容并找到“Bd”元素,它是一个边框。 将其 RowSpan 属性更改为“2”。
最后,将项目的“HorizontalContentAlignment”属性设置为“Stretch”(在项目上或通过样式,如果您需要将其应用到多个节点)。
您的物品现在应该具有正确的宽度。
现在,这仅适用于您选择的项目。 如果您希望它适用于添加到树视图的任何项目,则需要将树视图的“ItemContainerStyle”更改为新创建的样式,并删除 Blend 放置在 TreeViewItem 上的样式。
最后但并非最不重要的一点是,您需要将 TreeViewItem 的 ItemContainerStyle 设置为相同的样式,以便其子项也一直延伸,依此类推。
最后,使用您的示例和第一项上的子节点:
“TreeViewItemStyle1”是 Blend 为您创建的样式。
根据要求进行编辑
,这是由混合和修改生成的完整样式。 它很长,因为它基本上是内置样式的副本,稍加修改。
To achieve that you need to change the Control template of the TreeviewItem using the ItemContainerStyle of the TreeView (this is the style that gets applied to any item in the root of the treeview).
The default TreeViewItem is not stretched, so it does not extend all the way to the right.
When you set the Header, it is inside the TreeViewItem and so cannot extend past it.
I will not post the whole style because it would be way too long.
Here's what to do in Blend:
select your TreeViewItem, right click and chose "Edit Control Parts/Edit a copy". Save the style wherever you want.
Now, in the template, expand the stuff and locate the "Bd" element, which is a border. Change its RowSpan property to "2".
Last, set the "HorizontalContentAlignment" property of your item to "Stretch" (either on the item or through the style if you need to apply that to several nodes).
Your item should now be the correct width.
Now, this only applies to the item you selected. If you want that to work for any item you add to the treeview, you need to change the "ItemContainerStyle" of the Treeview to the newly created style, and remove the style that Blend placed on the TreeViewItem.
Last but not least, you need to set the ItemContainerStyle of your TreeViewItem to that same style so that its children also extend all the way, and so on and so forth.
So in the end, with your example and a child node on the first item:
The "TreeViewItemStyle1" is the style that Blend created for you.
EDIT
as requested, here's the full style as generated by blend and modified. It is long because it basically is a copy of the built-in style with minor modifications.
请参阅这篇文章了解两个示例。 我今天刚刚创建了这些。
在 WPF 中突出显示整个 TreeViewItem 行
See this post for two samples. I just created these today.
Highlight whole TreeViewItem line in WPF