flcoder
flcoder

Reputation: 710

UWP XAML Intellisense DataTemplate.DataType

Why does intellisense filter out interfaces and abstract classes? If I set DataType to an abstract class, it seems to still work fine. Perhaps this is just a bug? Also, related, inside DataTemplate, when I try to {x:Bind} it filters out inherited properties, so if I have Item : Base, and Base has a property Name, and DataType="Item", it filters out property Name and if I use it anyway, it seems to resolve to the class name. Did I miss something in the docs? Should I be making special non-abstract wrapping classes for every type I want to bind to xaml controls?

Upvotes: 1

Views: 364

Answers (1)

dear_vv
dear_vv

Reputation: 2358

After my testing, it seems that inherited interface-properties are not recognized by the compiler when using the X:Bind. But it applies to abstract classes.

You could follow the sample to check your steps.

XAML code:

<ListView x:Name="List" ItemsSource="{x:Bind Fruits}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Fruit">
                    <TextBlock Text="{x:Bind price}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
</ListView>

Code behind:

public sealed partial class MainPage : Page
{
    public ObservableCollection<Fruit> Fruits{get;set;}
    public MainPage()
    {
        this.InitializeComponent();
        Fruits = new ObservableCollection<Fruit>()
        {
            new Fruit(){name="apple",price=12},
            new Fruit(){name="peach",price=15},
            new Fruit(){name="pear",price=8},
            new Fruit(){name="banana",price=31},
            new Fruit(){name="grape",price=5}   
        };        
    }
}
public class Fruit: IFruit
{
    public string name { get; set;}
}
public abstract class IFruit
{    
    public int price { get; set;}
}

Upvotes: 1

Related Questions