Reputation: 99
I have class, that selects the ItemDataTemplate for objects. But I can't reference it in my XAML code. (Page.Resources).
It's the Items page in XAML. The class is in the commons folder, and I've referenced the commons folder here:
xmlns:common="using:Sample_App.Common"
and then when I wan't to add it to my XAML:
<common:MyDataTemplateSelector x:Key="Selector" AdTemplate="{StaticResource Ad}" NormalTemplate="{StaticResource Normal}"></common:MyDataTemplateSelector>
I get the following error:
The name "MyDataTemplateSelector" does not exist in the namespace "using:MyDataSelector"
Here's the MyDataSelector class:
namespace MyDataSelector
{
private class MyDataTemplateSelector : DataTemplateSelector
{
public DataTemplate NormalTemplate { get; set; }
public DataTemplate AdTemplate{ get; set; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is TestApp.Mainpage.NormalData)
return NormalTemplate
if (item is TestApp.Mainpage.AdData)
return AdTemplate;
return SelectTemplateCore(item, container);
}
}
}
Upvotes: 2
Views: 8640
Reputation: 1964
You have a couple problems with your code. First of all, you mention that your class is in "the commons folder" - this is totally irrelevant. The location of a code file generally doesn't matter to the compiler, but the namespace you declare the class in does matter.
namespace MyDataSelector // <- This is where your class can be found
{
private class MyDataTemplateSelector : DataTemplateSelector
{
So since your class is in the namespace MyDataSelector
, the reference in your xaml files should look something like this:
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:myDataSelector="using:MyDataSelector">
And you would reference your class like this:
<myDataSelector:MyDataTemplateSelector />
Another issue is that your class is declared as private. That doesn't make sense and probably won't compile. Remove private
to make your class internal, or change it to public
.
Upvotes: 3