简体   繁体   English

WinRT:如何在ContentControl的DataTemplateSelector中访问父项的ItemsSource

[英]WinRT: How to access parent's ItemsSource in DataTemplateSelector of ContentControl

i need help for my WindowsStoreApp using XAML/C#. 我需要使用XAML / C#的WindowsStoreApp帮助。

Let's start with a very short explanation of my Problem, and if it's not clear enough, then you'll find very detailed information below :) 让我们从我的问题的简短解释开始,如果还不够清楚,那么您将在下面找到非常详细的信息:)

In short: I have a ContentControl within a GridView . 简而言之:我在GridView有一个ContentControl The GridView has the ItemsSource set to a Collection. GridViewItemsSource设置为Collection。 In my ContentControl i want to use a selfmade DataTemplateSelector . 在我的ContentControl我想使用自制的DataTemplateSelector In it's SelectTemplateCore -method i need acces to the items of the GridView 's ItemsSource - how can i do that in WinRT? 在它的SelectTemplateCore方法中,我需要访问GridViewItemsSource -如何在WinRT中做到这一点?

In detail: 详细:

I want to display folders of the user's system, like the Picture's Library, connected USB devices and so on. 我想显示用户系统的文件夹,例如图片库,已连接的USB设备等。 Different types of folders shall get different icons. 不同类型的文件夹将获得不同的图标。 Some of the icons i have as XAML Path Data, and some as image files. 我拥有的某些图标作为XAML路径数据,而另一些则作为图像文件。 The folders are stored in a Collection. 文件夹存储在集合中。 This Collection (named "Ordner") is stored in the Page's Viewmodel. 此集合(名为“ Ordner”)存储在页面的Viewmodel中。 Each folder has a property (based on enum) that specifies its type. 每个文件夹都有一个属性(基于枚举),用于指定其类型。 First i use a custom DataTemplateSelector to check whether the folder's type can be displayed via image or via path. 首先,我使用自定义DataTemplateSelector来检查是否可以通过图像或路径显示文件夹的类型。 It works perfectly. 它运作完美。

The code for this is here... I have a Page like this: 代码在这里...我有一个这样的页面:

<common:LayoutAwarePage [...]
    xmlns:common="using:De.Mms.DiplomSnma.Win8App.Common">
    <common:LayoutAwarePage.Resources>
        <common:OrdnerDataTemplateSelector x:Key="ordnerTemplateSelector"
            TemplatePathquelle="{StaticResource PathquelleDataTemplate}"
            TemplateBildquelle="{StaticResource BildquelleDataTemplate}" />
    </common:LayoutAwarePage.Resources>

    <GridView ItemsSource="{Binding Ordner}" ItemTemplateSelector="{StaticResource ordnerTemplateSelector}" >
        [...]
    </GridView>
</common:LayoutAwarePage>


class OrdnerDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate TemplateBildquelle { get; set; }
    public DataTemplate TemplatePathquelle { get; set; }

    protected override DataTemplate SelectTemplateCore
        (object item, DependencyObject container)
    {
        OrdnerViewModel ordner = item as OrdnerViewModel;
        if (ordner.OrdnerTyp.Equals(EnumClass.OrdnerTyp.Sharepoint))
        {
            return TemplateBildquelle;
        }
        else
        {
            return TemplatePathquelle;
        }
    }
}

If the folder's icon is based on a Path, a DataTemplate called "PathquelleDataTemplate" in a ResourceDictionary is called. 如果文件夹的图标基于路径,则将调用ResourceDictionary名为“ PathquelleDataTemplate”的DataTemplate模板。 Within the DataTemplate is, besides other UI-elements my ContentControl . 除了其他UI元素外, DataTemplate内还包含我的ContentControl Here i wanted to use another custom made DataTemplateSelector to check again the folder's type and choose an according DataTemplate , which then contains the correct path and it's data. 在这里,我想使用另一个定制的DataTemplateSelector再次检查文件夹的类型,然后选择一个相应的DataTemplate ,然后包含正确的路径及其数据。 But of course i could not just use item in the overwritten SelectTemplateCore(item, container) . 但是当然我不能只在覆盖的SelectTemplateCore(item, container)使用item Without any DataContext the item is null . 没有任何DataContextitemnull How can i reference the ItemsSource of my (parent-parent-)parent GridView ? 我如何引用我的(parent-parent-)parent GridViewItemsSource I'm totally failing with specifying the DataContext . 我完全没有指定DataContext Every piece of help i found is based on WPF and seems not working in WinRT. 我发现的每一项帮助都是基于WPF的,似乎无法在WinRT中使用。 Or is there a completely different but better suited way to get access to the Collection? 还是有一种完全不同但更适合的方式来访问馆藏?

Here is the DataTemplate "PathquelleDataTemplate" (in a ResourceDictionary). 这是DataTemplate“ PathquelleDataTemplate”(在ResourceDictionary中)。

<DataTemplate x:Key="PathquelleDataTemplate">
    <Grid Height="160" Width="220">
        <StackPanel Orientation="Vertical" Background="White">
            [...]
            <StackPanel Orientation="Horizontal">
                <ContentControl ContentTemplateSelector="{StaticResource iconTemplateSelector}" />
                [...]
            </StackPanel>
        </StackPanel>
    </Grid>
</DataTemplate>

And here just as an example one of the DataTemplates the DataTemplateSelector would choose: 这里仅作为示例,DataTemplateSelector将选择以下其中之一:

<DataTemplate x:Key="BildOrdnerIconCCTemplate">
    <Path Fill="Black" Data="[...ThePathData...]"/>
</DataTemplate>

Thanks in advance for every help and sorry, if my english sounds sometimes a bit strange :) 在此先感谢您的帮助和对不起,如果我的英语有时听起来有些奇怪:)

Set the Content to the DataContext being passed in to PathquelleDataTemplate , ie Content设置为要传递给PathquelleDataTemplateDataContext ,即

<ContentControl ContentTemplateSelector="{StaticResource iconTemplateSelector}" Content="{Binding}"/>

As you noted, iconTemplateSelector will get two items, null, then the property. 如您所述, iconTemplateSelector将获得两个项目,null,然后是属性。 The null is for when the template first is applied and the second is for when the binding is resolved. null为第一个应用模板的时间,第二个为解决绑定的时间。

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

相关问题 ContentControl 的父视图模型中的访问属性 - Access Property in ContentControl's Parent ViewModel 如何在WinRt Xaml中访问UserControl的父资源 - How to access a UserControl's parent resources in WinRt Xaml ContentControl与DataTemplateSelector和绑定内容 - ContentControl with DataTemplateSelector and bound Content ContentControl上的绑定内容在DataTemplateSelector中为null - Binded content on ContentControl is null in DataTemplateSelector 如何将DataTemplateSelector与ContentControl结合使用以基于视图模型显示不同的控件? - How to use DataTemplateSelector with ContentControl to display different controls based on the view-model? 如何在TreeViewItem的模板上使用DataTemplateSelector? - How to use DataTemplateSelector on TreeViewItem's template? 访问Itemscontrol的ItemsSource中的对象 - Access objects in Itemscontrol's ItemsSource 实现DataTemplateSelector-想以某种方式传递父级的属性 - Implementing DataTemplateSelector - want to pass parent's property somehow 引用父DataTemplateSelector的DataTemplateSelector中的DataTemplate? - DataTemplate within a DataTemplateSelector referencing the parent DataTemplateSelector? 如何获取ContentControl中托管的UserControl的父级? - How to get the parent of a UserControl hosted in a ContentControl?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM