Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot override inherited member SelectTemplate... with code I copied from MSDN

I have an override problem. I know you can't override inherited members unless they are marked virtual, abstract or override, but...

I have this code from msdn:

using System.Windows;
using System.Windows.Controls;

namespace SDKSample
{
    public class TaskListDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate
            SelectTemplate(object item, DependencyObject container)
        {
            FrameworkElement element = container as FrameworkElement;

            if (element != null && item != null && item is Task)
            {
                Task taskitem = item as Task;

                if (taskitem.Priority == 1)
                    return
                        element.FindResource("importantTaskTemplate") as DataTemplate;
                else 
                    return
                        element.FindResource("myTaskTemplate") as DataTemplate;
            }

            return null;
        }
    }
}

And I added this code:

public class PostTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        ListViewItem lvi = item as ListViewItem;

        AppNetClient.PostClass key = lvi.DataContext as AppNetClient.PostClass;

        if (key.postTemplate == "Post")
        {
            return lvi.FindName("PostDataTemplate") as DataTemplate;
        }
        else
        {
            return lvi.FindName("SelectedPostDataTemplate") as DataTemplate;
        }
    }
}

And I get this error:

Error   3   'oneSocialLife.MainPage.PostTemplateSelector.SelectTemplate(object, Windows.UI.Xaml.DependencyObject)': cannot override inherited member 'Windows.UI.Xaml.Controls.DataTemplateSelector.SelectTemplate(object, Windows.UI.Xaml.DependencyObject)' because it is not marked virtual, abstract, or override   C:\Users\kcone\documents\visual studio 2012\Projects\HttpClientApp\HttpClientApp\MainPage.xaml.cs   339 42  oneSocialLife

I've spent hours looking for why, but can't figure it out. Any ideas?

like image 860
Ken Cone Avatar asked Dec 26 '22 16:12

Ken Cone


2 Answers

The sample you're referencing is for WPF (System.Windows.Controls.DataTemplateSelector).

In a Windows Store app, you're using Windows.UI.Xaml.Controls.DataTemplateSelector which has different semantics, and SelectTemplate is indeed not overridable, but SelectTemplateCore is.

like image 147
Jim O'Neil Avatar answered Mar 09 '23 01:03

Jim O'Neil


The code that ended up working: from: http://msdn.microsoft.com/en-us/library/ms742521.aspx also: http://www.switchonthecode.com/tutorials/wpf-tutorial-how-to-use-a-datatemplateselector

public class PostTemplateSelector : DataTemplateSelector
{
    public DataTemplate PostTemplate { get; set; }
    public DataTemplate SelectedPostTemplate { get; set; }

    protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
    {
        //ListViewItem lvi = item as ListViewItem;
        //AppNetClient.PostClass key = lvi.DataContext as AppNetClient.PostClass;

        AppNetClient.PostClass key = item as AppNetClient.PostClass;

        if (key.postTemplate == "Post")
        {
            return PostTemplate;
        }
        else
        {
            return SelectedPostTemplate;
        }
    }
}
like image 43
Ken Cone Avatar answered Mar 08 '23 23:03

Ken Cone