更改gridviewitem的绑定源

时间:2016-05-23 15:13:13

标签: c# binding grid uwp uwp-xaml

我有一个GridView ItemsSource绑定到StreamCollections列表。在ItemsTemplateSelector中,我根据DataTemplate的值更改了StreamCollection

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的约束力;同样适用于TrackFrameworkElement元素的类型为GridViewItem,但我似乎无法找到对BindingSource的任何引用。

1 个答案:

答案 0 :(得分:0)

不确定它是否有帮助,但当我有类似需求时,我已从应用程序中删除了字典:

 App.Current.Resources.MergedDictionaries.Remove(App.Current.Resources.MergedDictionaries.Last());

并添加新的

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("ms-appx:AppStyle1.xaml");
Application.Current.Resources.MergedDictionaries.Add(dict);

因此,您可以尝试将模板放入字典中并以这种方式替换