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?
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.
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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With