UWP 绑定到 ListView 内的方法

UWP binding to a method inside a ListView

如何在已经将 x:DataType 设置为 class 的绑定中调用自定义方法?报错信息显示:The 属性 'TotalMembers' was not found in type 'Family'. When adding the method from the page load to the Family class,错误依旧。

// model
public class Family
{
    public int ID { get; set; }
    public string Surname { get; set; }
    public int TotalMembers() { return "some code..." }
}
public class Person
{
    public int ID { get; set; }
    public int FamilyID { get; set; }
    public string FirstName { get; set; }
}

// page load
public ObservableCollection<Family> ocFamily { get; set; }
ocFamily = new ObservableCollection<Family>();

// xaml
<ListView ItemsSource="{x:Bind ocFamily}">
    <ListView.ItemTemplate>
            <DataTemplate x:DataType="data:Family>
            <StackPanel>
                <TextBlock Text="{x:Bind Surname}" />
                <TextBlock Text="{x:Bind TotalMembers()}" />
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

第二个文本块应显示家庭成员总数。

To use functions with {x:Bind}, your app's minimum target SDK version must be 14393 or later. You can't use functions when your app targets earlier versions of Windows 10.

您需要将 最低版本 设置为至少 14393 以便 x:绑定到函数 上班。

您可以从 here 阅读更多内容。

在您的情况下,您应该能够创建 TotalMembers 属性 而不是 方法 .