HierarchicalDataTemplate treeViewitems不可单击

时间:2018-02-05 15:55:01

标签: c# wpf xaml treeview visual-studio-extensions

我有以下XAML创建树视图并使用TreeViewItems填充它。 转换器只需获取名称并使用2个不同颜色的字符串向后吐出StackPanel。 我发现我实际上可以直观地使用该堆栈面板的唯一方法是将其设置在TreeViewItem的标题中,但这并不是最好的,因为TreeViewItem已经以编程方式创建。

结果是我无法点击标签(标题)但它看起来很花哨,但是我发现标签前面有一个很大的空间我可以点击,然后必须来自生成的TreeViewItem。 我真的需要标签包含2个文本块的堆栈面板,有没有解决方案?

<UserControl.Resources>
        <XmlDataProvider x:Key="MyXmlProvider" Source="LogansTest.xml" XPath="/Items"/>
        <customScripts:NameGeneration x:Key="NameGeneration"/>
        <HierarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding XPath=./*}">
            <TreeViewItem x:Name="nodetext"/>

            <HierarchicalDataTemplate.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter TargetName="nodetext" Property="Header" Value="{Binding Converter={StaticResource NameGeneration}}"/>
                </DataTrigger>
            </HierarchicalDataTemplate.Triggers>
        </HierarchicalDataTemplate>
    </UserControl.Resources>

修改

按照mm8的建议,我已经编辑了我的代码,我已经完整地上传了我的XAML,它不再生成树,我对XAML很新,所以我我不知道自己做错了什么,呵呵。 进行一些测试,转换器没有被调用,树视图在它拥有XML文件的所有节点之前就是空的

<UserControl x:Class="XmlOutline.OutlineWindowControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
             xmlns:customScripts="clr-namespace:XmlOutline.CustomScripts"
             Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
             Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             Name="MyToolWindow">
    <UserControl.Resources>
        <XmlDataProvider x:Key="MyXmlProvider" Source="LogansTest.xml" XPath="/Items"/>
        <customScripts:NameGeneration x:Key="NameGeneration"/>
    </UserControl.Resources>


    <Grid x:Name="TreeGrid" DataContext="MyXmlProvider">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TreeView Name="TreeItems" Visibility="Hidden"
                  ItemsSource="{Binding Source={StaticResource MyXmlProvider}}"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  VirtualizingStackPanel.IsVirtualizing="False"
                  VirtualizingStackPanel.VirtualizationMode="Standard"
                  Background="#252525"
                  SelectedItemChanged="TreeView_OnSelectedItemChanged">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding XPath=./*}"/>
            </TreeView.ItemTemplate>
            <TreeView.ItemContainerStyle>
                <Style TargetType="TreeViewItem">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                            <Setter Property="Header" Value="{Binding Converter={StaticResource NameGeneration}}"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TreeView.ItemContainerStyle>
        </TreeView>
    </Grid>
</UserControl>

修改 这是在打开新的XML文档时设置新数据源的代码。

var provider = new XmlDataProvider()
{
    Source = new Uri(gotFocus.Document.Path + gotFocus.Document.Name),
    XPath = "./*"
};

OutlineWindowInstance.TreeItems.DataContext = provider;

顺便说一句,可以在这里找到整个GIT仓库: https://github.com/LoganLabster/VsXmlOutline

2 个答案:

答案 0 :(得分:1)

您不应在TreeViewItem中创建另一个HierarchicalDataTemplate容器。尝试定义设置ItemContainerStyle属性的Header

<TreeView ItemsSource="{Binding Source={StaticResource MyXmlProvider}}">
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding XPath=./*}" />
    </TreeView.ItemTemplate>
    <TreeView.ItemContainerStyle>
        <Style TargetType="TreeViewItem">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                    <Setter Property="Header" Value="{Binding Converter={StaticResource NameGeneration}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>
</TreeView>

答案 1 :(得分:0)

感谢@ mm8我设法找到了答案,一旦我弄明白它(这花了太长时间,doh)就非常简单了。 我没有将StackPanel设置为treeviewitem标头,而是在XAML中构建StackPanel并一次设置一个值,瞧,它有效。

<UserControl x:Class="XmlOutline.OutlineWindowControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:vsshell="clr-namespace:Microsoft.VisualStudio.Shell;assembly=Microsoft.VisualStudio.Shell.15.0"
             xmlns:customScripts="clr-namespace:XmlOutline.CustomScripts"
             Background="{DynamicResource {x:Static vsshell:VsBrushes.WindowKey}}"
             Foreground="{DynamicResource {x:Static vsshell:VsBrushes.WindowTextKey}}"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             Name="MyToolWindow">
    <UserControl.Resources>
        <XmlDataProvider x:Key="MyXmlProvider" Source="LogansTest.xml" XPath="/Items"/>
        <customScripts:NameGeneration x:Key="NameGeneration"/>
    </UserControl.Resources>

    <Grid x:Name="TreeGrid" DataContext="MyXmlProvider">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <TreeView Name="TreeItems" Visibility="Hidden"
                  ItemsSource="{Binding}"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  VirtualizingStackPanel.IsVirtualizing="False"
                  VirtualizingStackPanel.VirtualizationMode="Standard"
                  Background="#252525"
                  SelectedItemChanged="TreeView_OnSelectedItemChanged"
                  TreeViewItem.Expanded="TreeViewItem_Expanded"
                  TreeViewItem.Collapsed="TreeViewItem_Collapsed">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate x:Name="myTest" ItemsSource="{Binding XPath=./*}">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock x:Name="Title"    Foreground="LightSkyBlue" FontSize="14"/>
                        <TextBlock x:Name="SubTitle" Foreground="YellowGreen"  FontSize="13"/>
                    </StackPanel>
                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                            <Setter TargetName="Title" Property="Text"  Value="{Binding Path=Name}"/>
                            <Setter TargetName="SubTitle" Property="Text" Value="{Binding Converter={StaticResource NameGeneration}}" />
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</UserControl>