[英]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. GridView
将ItemsSource
设置为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
方法中,我需要访问GridView
的ItemsSource
-如何在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
. 没有任何
DataContext
的item
为null
。 How can i reference the ItemsSource
of my (parent-parent-)parent GridView
? 我如何引用我的(parent-parent-)parent
GridView
的ItemsSource
? 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
设置为要传递给PathquelleDataTemplate
的DataContext
,即
<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.