【问题标题】:How to nest ItemsControl with multiple ItemsSource?如何使用多个 ItemsSource 嵌套 ItemsControl?
【发布时间】:2021-07-23 08:50:26
【问题描述】:

我有一个带有名称的ConfigList 对象和一个Dictionary,我需要将ItemsControls 与不同的ItemsSource 嵌套。

我试图这样做:

<ItemsControl x:Name="TestStep" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=ConfigList }" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Ictrl.Nom}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    <ItemsControl
    ItemsSource="{Binding Path=Param}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Key}" />
                    <TextBlock Text=" : " />
                    <TextBlock Text="{Binding Path=Value}" />
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</ItemsControl>

当我启动我的应用程序时,我遇到了这个错误:

System.Windows.Markup.XamlParseException : '' 向 'System.Windows.Controls.ItemCollection' 类型的集合抛出了一个 例外。 ' 行号 '25' 和行位置 '14'。 '

内部异常 InvalidOperationException: ItemsSource 在时的无效操作 利用。使用 ItemsControl.ItemsSource 访问和编辑项目。

知道问题出在哪里吗?

【问题讨论】:

  • 同时使用ItemsSourceItems 会导致异常,这是被禁止的。在 XAML 中添加嵌套 ItemsControl 的方式隐式地将其添加为其 Items 集合的元素。这是不允许的,因为您已经绑定到 ItemsSource。您可能希望将嵌套的 ItemsControl 添加到 DataTemplate。

标签: c# wpf xaml data-binding


【解决方案1】:

您在ItemsControl 上设置了ItemsSource。数据模板用于创建显示数据的控件。然后将创建的项目放入ItemsControlItems 集合中。如果直接在 XAML 中将元素添加到 ItemsControl,这也会将它们放入 Items 集合中。两者都做是不允许的。您可以指定 ItemsSource 或直接添加到 Items。来自documentation

请注意,您可以使用 Items 或 ItemsSource 属性来指定用于生成 ItemsControl 内容的集合。设置ItemsSource 属性后,Items 集合变为只读且大小固定

但是,在您的情况下,这不是真正的问题,因为您的标记对于您想要实现的目标是错误的。如果您真的打算嵌套ItemsControls,您只需将外部ItemsControl 的数据模板更改为包含另一个绑定到外部数据项内的集合属性的ItemsControl。由于已经有TextBox,您必须使用面板(例如StackPanel)在模板中托管多个控件。

<ItemsControl x:Name="TestStep" Grid.Row="0" Grid.Column="0" ItemsSource="{Binding Path=ConfigList }" HorizontalAlignment="Center" VerticalAlignment="Center">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Path=Ictrl.Nom}" />
                <ItemsControl ItemsSource="{Binding Path=Param}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="{Binding Path=Key}" />
                                <TextBlock Text=" : " />
                                <TextBlock Text="{Binding Path=Value}" />
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

如果您想获得数据的分层视图,使用TreeView 可能更合适。

【讨论】:

  • 感谢您的详细回答,我理解我的错误。现在可以了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多