在这里学习 WPF,我正试图了解分层数据绑定。
这是我的情况:
public class A
{
public int Id { ... }
public IEnumerable<B> Children { ... }
}
public class B
{
public string SomeValue { ... }
}
我想使用 aItemsControl
来显示一个集合,A
并且对于A
我想要一个内部ItemsControl
显示的每次出现A.Children
。
我认为这可以解决问题,但显然,我还有很多东西要学……
<ItemsControl x:Name="icCollectionOfAs" ItemsSource="{Binding}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Width="auto" Height="auto">
<TextBlock Text="{Binding Id}" />
<ItemsControl ItemsSource="{Binding Children}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SomeValue}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
然后,在代码隐藏中......
icCollectionOfAs.ItemsSource = createSomeAsWithChildBsInThem();
这一切的结果是什么都没有显示出来。为什么?
谢谢