具有ControlTemplate的HierarchicalDataTemplate和HeaderedItemsControl无法显示嵌套数据 [英] HierarchicalDataTemplate and HeaderedItemsControl with ControlTemplate fails to show nested data

查看:60
本文介绍了具有ControlTemplate的HierarchicalDataTemplate和HeaderedItemsControl无法显示嵌套数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使这种模式与MenuTreeView一起使用,但是当我尝试使用HeaderedItemsControl时,我一定会丢失一些东西:

I can get this pattern to work with Menu and TreeView but I must be missing something when I make an attempt with HeaderedItemsControl:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
<Page.Resources>

    <HierarchicalDataTemplate x:Key="MenuItemTemplate" ItemsSource="{Binding XPath=foo}">
        <AccessText Text="{Binding XPath=@a}" />
    </HierarchicalDataTemplate>

    <Style TargetType="HeaderedItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type HeaderedItemsControl}">
                    <StackPanel>
                        <ContentPresenter ContentSource="Header"/>
                        <ItemsPresenter Margin="10,0,0,0" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <XmlDataProvider x:Key="RootXml" XPath="/root/foo">
        <x:XData>
            <root xmlns="">
                <foo a="one">
                    <foo a="two" b="wow, two" />
                    <foo a="three" b="wow, three" />
                    <foo a="four" b="wow, four" />
                </foo>
                <foo a="one again">
                    <foo a="two others" b="wow, two others" />
                    <foo a="three the hard way" b="wow, three again" />
                </foo>
            </root>
        </x:XData>
    </XmlDataProvider>

</Page.Resources>

<StackPanel>
    <HeaderedItemsControl
        Header="My Foo List"
        ItemTemplate="{Binding Source={StaticResource MenuItemTemplate}}"
        ItemsSource="{Binding Source={StaticResource RootXml}}">
    </HeaderedItemsControl>
</StackPanel>
</Page>

在XamlPadX中,这显示:

In XamlPadX, this shows:

My foo list
    one
    one again

我需要对ControlTemplate做些什么才能使数据正确显示?还是我需要更详尽的(或附加的)HierarchicalDataTemplate?另外:我们如何显示foo/@b数据?

Do I need to do something with the ControlTemplate to get the data to display correctly? Or do I need a more elaborate (or an additional) HierarchicalDataTemplate? Also: how do we show the foo/@b data?

推荐答案

这是一个较晚的答案.也在尝试弄清楚这一点,但对上述答案和/或链接的答案中的推理不满意,因为所有控件之间的XAML模式应该相同.

Here is a late answer. Been trying to figure this out as well, but was not satisfied with the reasoning in the above answer and/or the linked answer, as the XAML pattern should be the same between all controls.

经过一会儿与jetbrains dotPeek的交流,并且梳理了一下TreeView控件,答案终于很简单了. TreeView和TreeViewItem将 IsItemItsOwnContainerOverride GetContainerForItemOverride 覆盖到将保留子项的控件(在TreeView Case中为TreeViewItem).您可以创建两个简单的自定义控件来处理此问题.

After a bit of time with jetbrains dotPeek, and combing thought the TreeView Control the answer is finally quite simple. The TreeView and TreeViewItem override IsItemItsOwnContainerOverride and GetContainerForItemOverride to the control which will hold the children (TreeViewItem in the TreeView Case). You can create two simple custom controls to handle this.

您的 HeaderedItemControl 类将如下所示:

public class MyHierarchicalViewItem : HeaderedItemsControl
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is MyHierarchicalViewItem;
    }

    protected override DependencyObject GetContainerForItemOverride()
    {
        return (DependencyObject)new MyHierarchicalViewItem();
    }
}

您的ItemControl(相当于TreeView或Menu)将是这样:

Your ItemControl (Equivalent to the TreeView or Menu) would be this:

public class MyHierarchicalView:ItemsControl 
{
    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is MyHierarchicalViewItem;
    }

    protected override DependencyObject GetContainerForItemOverride()
    {

        return (DependencyObject) new MyHierarchicalViewItem();
    }
}

您的XAML基本上相同,只是引用正确的控件并添加适当的名称空间(我的案例xmlns:myControls ="clr-namespace:").

Your XAML would be largely the same, just putting referencing the correct control and adding the appropriate namespace (my case xmlns:myControls="clr-namespace:").

<UserControl.Resources>

        <HierarchicalDataTemplate x:Key="MenuItemTemplate" ItemsSource="{Binding XPath=foo}">
            <AccessText Text="{Binding XPath=@a}" />
        </HierarchicalDataTemplate>

        <Style TargetType="{x:Type myControls:MyHierarchicalViewItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type myControls:MyHierarchicalViewItem}">
                        <StackPanel>
                            <ContentPresenter ContentSource="Header"/>
                            <ItemsPresenter Margin="10,0,0,0" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <XmlDataProvider x:Key="RootXml" XPath="/root/foo">
            <x:XData>
                <root xmlns="">
                    <foo a="one">
                        <foo a="two" b="wow, two" />
                        <foo a="three" b="wow, three" />
                        <foo a="four" b="wow, four" />
                    </foo>
                    <foo a="one again">
                        <foo a="two others" b="wow, two others" />
                        <foo a="three the hard way" b="wow, three again" />
                    </foo>
                </root>
            </x:XData>
        </XmlDataProvider>

        </UserControl.Resources>

    <StackPanel>
        <myControls:MyHierarchicalViewItem
        Header="My Foo List"
        ItemTemplate="{Binding Source={StaticResource MenuItemTemplate}}"
        ItemsSource="{Binding Source={StaticResource RootXml}}">
        </myControls:MyHierarchicalViewItem>
    </StackPanel>

</UserControl>

这篇关于具有ControlTemplate的HierarchicalDataTemplate和HeaderedItemsControl无法显示嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆