0
我遇到了通過父類列表顯示子類屬性的問題。父類的列表中的數據綁定ListView子類屬性
的XAML
<ListView Grid.Row="0" Grid.RowSpan="4" Grid.Column="0" x:Name="lvTest" ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Auto" SelectionMode="Single" ScrollViewer.VerticalScrollBarVisibility="Auto" Width="630" Height="270">
<ListView.View>
<GridView>
<GridViewColumn Header="Index" DisplayMemberBinding="{Binding Path=index}" Width="100" />
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=c.name}" Width="100" />
<GridViewColumn Header="age" DisplayMemberBinding="{Binding Path=c.age}" Width="100" />
</GridView>
</ListView.View>
</ListView>
代碼隱藏
public partial class MainWindow : Window
{
List<parent> parentList;
public MainWindow()
{
InitializeComponent();
GenerateList();
lvTest.ItemsSource = parentList;
}
private void GenerateList()
{
parentList = new List<parent>();
for (int i = 0; i < 10; i++)
{
parent p = new parent();
p.index = i;
child c = new child();
c.name = "Name_" + (i + 1).ToString();
c.age = i;
parentList.Add(p);
}
}
}
類
public class parent
{
public int index { get; set; }
public child c { get; set; }
}
public class child
{
public string name { get; set; }
public int age { get; set; }
}
我無法顯示「名」和「年齡」的財產孩子班,但我在訪問索引父級類沒有問題。
任何人都知道該怎麼做?
噢亞。愚蠢的錯誤。謝謝 – user1599647
別擔心,我們都有時會犯錯誤:)請標記爲答案,如果它會幫助你。 – Tony