I am having problem displaying child class properties through a list of parent class.
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>
Code Behind
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);
}
}
}
Classes
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; }
}
I can't display the "name" and "age" property of the child class but I have no problem in accessing the index property of the parent class.
Anyone knows how to do it?
It's seems you forget to "save" you child
instance in parent
object:
child c = new child();
c.name = "Name_" + (i + 1).ToString();
c.age = i;
p.c = c; // THIS!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.