简体   繁体   English

如何在XAML TreeView HierarchicalDataTemplate中垂直对齐项目符号?

[英]How can I vertically align the bullet in a XAML TreeView HierarchicalDataTemplate?

When a node in my TreeView has multiple lines, the TreeView bullet gets vertically centered. 当TreeView中的节点有多行时,TreeView子弹将垂直居中。

How can I top-align the TreeView bullet? 如何使TreeView子弹顶对齐?

alt text http://www.deviantsart.com/upload/1uh2k8p.png 替代文字http://www.deviantsart.com/upload/1uh2k8p.png

<pages:BasePage.Resources>
    <data:HierarchicalDataTemplate x:Key="OutlineTemplate"
        ItemsSource="{Binding OutlineDocumentObjects}">
        <TextBlock Text="{Binding Line}" 
            TextWrapping="Wrap" 
            VerticalAlignment="Top"
            Width="600"/>
    </data:HierarchicalDataTemplate>
</pages:BasePage.Resources>

<StackPanel Style="{StaticResource StackPanelPageWrapperStyle}">
    <tk:TreeView x:Name="TheTreeView" 
        ItemsSource="{Binding TheOutline.OutlineDocumentObjects}"
        ItemTemplate="{StaticResource OutlineTemplate}">
    </tk:TreeView>
</StackPanel>

Good question... of course, it could be done by redefining the template, but it's a pain... (if you want to go that way, extract the template with StyleSnooper or ShowMeTheTemplate and change the VerticalAlignment of the ToggleButton ) 好问题......当然,可以通过重新定义模板来完成,但这很痛苦......(如果你想这样做,用StyleSnooper或ShowMeTheTemplate提取模板并改变ToggleButtonVerticalAlignment

Another way is to inherit TreeViewItem and override the OnApply method. 另一种方法是继承TreeViewItem并覆盖OnApply方法。 Since the ToggleButton has a name ("Expander") in the default template, you can find it and apply the VerticalAlignment you want : 由于ToggleButton在默认模板中有一个名称(“Expander”),您可以找到它并应用所需的VerticalAlignment

public class TopAlignedTreeViewItem : TreeViewItem
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        if (Template != null)
        {
            ToggleButton btn = Template.FindName("Expander", this) as ToggleButton;
            if (btn != null)
            {
                btn.VerticalAlignment = VerticalAlignment.Top;
            }
        }
    }
}

For the TreeView to generate TopAlignedTreeViewItem s instead of TreeViewItem s, you also need to make your own TreeView : 要使TreeView生成TopAlignedTreeViewItem而不是TreeViewItem ,您还需要创建自己的TreeView

public class TopAlignedTreeView : TreeView
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return (item is TopAlignedTreeViewItem);
    }

    protected override System.Windows.DependencyObject GetContainerForItemOverride()
    {
        return new TopAlignedTreeViewItem();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在WPF中在XAML中垂直对齐TextBlock和TextBox? - How do I vertically align a TextBlock and a TextBox in XAML in WPF? 如何将WPF TreeView HierarchicalDataTemplate与LINQ to Entities一起使用? - How do I use a WPF TreeView HierarchicalDataTemplate with LINQ to Entities? 如何使用 EntityCollection 对象的 HierarchicalDataTemplate 对 WPF TreeView 进行排序? - How do I sort a WPF TreeView using the HierarchicalDataTemplate for EntityCollection objects? 如何在 XAML 中将父级 ItemsHost 用于 HierarchicalDataTemplate? - How do I use parent's ItemsHost for HierarchicalDataTemplate in XAML? TreeView的HierarchicalDataTemplate - HierarchicalDataTemplate for TreeView 如何为 TreeView 项目重用 XAML 模板? - How can I reuse XAML templates for TreeView items? 如何从 treeview 上的 HierarchicalDataTemplate 中删除项目 - How to Remove an Item from HierarchicalDataTemplate on a treeview 如何将ViewModel的属性绑定到Treeview的HierarchicalDataTemplate中的元素? - How to bind a property of ViewModel to an element in HierarchicalDataTemplate of a Treeview? 如何垂直对齐FlowDocument的TableCell(或其内容) - How can I vertically align a TableCell (or its content) of a FlowDocument 如何在XAML中将不同的数据类型分配给静态HierarchicalDataTemplate? - How to assign different DataTypes to a static HierarchicalDataTemplate in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM