UWP XAML 智能感知 DataTemplate.DataType

时间:2021-02-26 22:07:52

标签: xaml uwp intellisense datatemplate

为什么智能感知会过滤掉接口和抽象类?如果我将 DataType 设置为抽象类,它似乎仍然可以正常工作。也许这只是一个错误?另外,在 DataTemplate 中,当我尝试 {x:Bind} 时,它会过滤掉继承的属性,因此如果我有 Item : Base,并且 Base 有一个属性 Name,并且 {{ 1}},它过滤掉属性 DataType="Item",如果我仍然使用它,它似乎解析为类名。我错过了文档中的某些内容吗?我是否应该为要绑定到 xaml 控件的每种类型制作特殊的非抽象包装类?

1 个答案:

答案 0 :(得分:0)

经过我的测试,在使用 X:Bind 时,编译器似乎无法识别继承的接口属性。但它适用于抽象类。

您可以按照示例检查您的步骤。

XAML 代码:

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

背后的代码:

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;}
}
相关问题