0

在这里学习 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();

这一切的结果是什么都没有显示出来。为什么?

谢谢

4

1 回答 1

2

您不应在 XAML ( ItemsSource="{Binding}") 和代码后面 ( icCollectionOfAs.ItemsSource = createSomeAsWithChildBsInThem();) 中都指定 ItemsSource,XAML 可能稍后会被调用。

其余的对我来说似乎很好,除了 ID 的 TextBlock 可能会隐藏在子项的 ItemsControl 后面,因为您使用没有行或列的 Grid 而不是 StackPanel。

于 2011-06-20T23:03:39.323 回答