WPF将子类绑定到Listview

时间:2015-01-02 14:22:41

标签: wpf listview bind subclass

我有这堂课:

public class Car
{
    public struct CarType
    {
        public string Manufacturer { get; set; }
        public string Model { get; set; }
        public int Year { get; set; }
    }

    public CarType Type;
    public string LicenseNumber { get; set; }
    public int Km { get; set; }
}

要将LicenseNumber或Km绑定到我的列表视图,我使用:

<ListView.View>
    <GridView>
        <GridViewColumn Width="140" DisplayMemberBinding="{Binding LicenseNumber}">
            <GridViewColumn.Header>
                <GridViewColumnHeader Click="GridViewColumnHeader_Click">License Number</GridViewColumnHeader>
            </GridViewColumn.Header>
        </GridViewColumn>
   </GridView>
</ListView.View>

命令DisplayMemberBinding="{Binding LicenseNumber}"绑定到LicenseNumber属性。

但如何绑定子类。喜欢Type.Manufacturer? Type.Model?

3 个答案:

答案 0 :(得分:1)

您也可以绑定到实例上的属性(只需使用您已熟悉的点符号):

<TextBlock Text="{Binding Path=Type.Manufacturer}" />

答案 1 :(得分:0)

你可能从未尝试过。

<GridViewColumn Width="140" DisplayMemberBinding="{Binding Type.Manufacturer}">

答案 2 :(得分:0)

您应该使用以下代码:

<ListView.View>
    <GridView>
        <GridViewColumn Width="140" DisplayMemberBinding="{Binding Type.Manufacturer}">
           ...