【发布时间】:2016-09-20 13:09:30
【问题描述】:
我有一个GridView,其中ItemsSource 绑定到StreamCollections 列表。在ItemsTemplateSelector 中,我根据StreamCollection 的值更改DataTemplate。
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;
if (element != null && item is StreamCollection)
{
StreamCollection stream = (StreamCollection) item;
if (stream.Playlist != null)
{
return Application.Current.Resources["PlaylistDataTemplate"] as DataTemplate;
}
if (stream.Track != null)
{
return Application.Current.Resources["TrackDataTemplate"] as DataTemplate;
}
}
return null;
}
如果Playlist 不是null,则GridViewItem 应该绑定Playlist; Track 相同。 FrameworkElement 元素的类型为 GridViewItem,但我似乎找不到对 BindingSource 的任何引用。
【问题讨论】:
-
XAML 代码是什么?您可以尝试不使用模板选择器,以确保您的 XAML 代码和绑定适用于其中一种元素类型(例如轨道)吗?
-
绑定适用于元素 Track 和播放列表。但在我的列表中,可以有 2 种不同的类型,曲目或播放列表。这就是为什么我制作一个绑定到 StreamCollection 的 GridView(它包含一个属性轨道和播放列表)
-
我了解您的列表和列表元素的绑定在使用 TemplateSelector 时不起作用。因此,您能否尝试不使用它来检查问题是否真的在这里。或者如果那不是您的问题,请详细说明您的问题?
-
请发布 xaml 代码
-
我测试了你发布的代码,它在我身边运行良好,但我很好奇你的
StreamCollection是什么,我认为这是你GridView的数据模型类,里面有两个属性Playlist和Track,这两个属性的数据类型是什么?当我试图重现您的问题时,我将这两个属性设置为List<string>。你把你的两个DataTemplates 放在哪里了?当您使用Application资源时,您可以将它们放入 App.xaml 文件中。如果仍有问题,请发布您的 xaml 代码和数据模型。
标签: c# binding grid uwp uwp-xaml