【问题标题】:WPF: Can I have multiple ItemsControls with different ItemsSources?WPF:我可以有多个具有不同 ItemsSources 的 ItemsControls 吗?
【发布时间】:2017-06-24 21:53:12
【问题描述】:

在我的 WPF 应用程序中,我希望有几个由用户在运行时生成的项目。这些来自不同的类,因此,我最初的想法是将它们添加到不同的 Observable 集合中,然后将它们用作 ItemsSources 或不同的 ItemsControls。但是,WPF 给了我错误System.InvalidOperationException:在使用 ItemsSource 之前,Items 集合必须为空。我不是 WPF 专家,但THIS SO 问题的答案似乎表明我只能拥有 1 个 ItemsControl。

THIS SO 问题表明也许我应该使用 CompositeCollection 类,但与引用的问题不同,我有几个完全不同的 Observable Collections 用于完全不同的任务。

这是我的 XAML.CS 的相关部分,其中包含两个集合:1 个自定义接口类型和 1 个自定义类类型

 public MainWindow()
    {           
        InitializeComponent();          
        DefaultWindowDefinition.ItemsSource = ProcessElements = new ObservableCollection<IProcessSimulator>();
        PathControl.ItemsSource = PathElements = new ObservableCollection<VisualPath>();           
    }

这是我尝试使用的 XAML 的相关部分:

<Grid           x:Name="MainGrid"
                Background="{StaticResource Alternating}"
                MouseLeftButtonUp="grid_MouseLeftButtonUp"
                ShowGridLines="True">
    <ItemsControl   Name="DefaultWindowDefinition"
                    ItemsSource="{Binding ProcessElements}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
               <!--HERE IS A LONG LIST OF ELEMENTS-->
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
               <!--TEMPLATE FOR THE 1ST ITEMSCONTROL-->
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <!--STYLE PROPERTIES FOR THE 1ST ITEMSCONTROL-->
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
    <ItemsControl Name="PathControl"
                    ItemsSource="{Binding PathElements}">
        <DataTemplate>
            <!--HERE IS A  LIST OF OTHER TYPE OFELEMENTS-->
        </DataTemplate>
    </ItemsControl>
</Grid>

我应该如何解决这个问题,或者更确切地说,我应该使用什么 C#/WPF 元素?一个参考和一些简单的解释就足够了,其余的我可以自己google,我只是不知道该寻找什么。

【问题讨论】:

  • 我看到的一个错误是您不需要在 XAML 中绑定ItemsSource,并且还需要在构造函数中设置它。只设置成员属性(ProcessElementsPathElements)就足够了。这可能会导致异常。如果不是这样,哪一行会抛出异常?
  • 这不是问题的根源,但在后面的代码中设置 ItemsSource only 似乎有效,所以感谢您指出这一点!

标签: wpf xaml observablecollection itemscontrol itemssource


【解决方案1】:

您似乎设置了两次 ItemsSource。一次在后面的代码中,一次在 XAML 中。删除设置 Items 源的代码,然后初始化 observable 集合。 XAML 应该负责绑定到集合。

【讨论】:

  • 好吧,我确实定义了两次,一次在代码后面: PathControl.ItemsSource = PathElements = new ObservableCollection();一次在 XAML 中: 但有趣的是,我以 very same 的方式使用我的第一个 Observable 集合,没有任何错误,我在代码隐藏中声明一次,然后在 XAML 中声明一次。事实上,如果我在 XAML 中声明它们 only 就没有任何效果。这很可能是我的 VS,因为它甚至不识别 XAML,所以到目前为止我没有使用颜色编码……是时候重新安装了。
【解决方案2】:

您应该将“PathControl”的 ItemTemplate 属性设置为您的 DataTemplate:

    <ItemsControl Name="PathControl" ItemsSource="{Binding PathElements}">
        <ItemsControl.ItemTemplate> <!-- NOTE THIS -->
            <DataTemplate>
                <!--HERE IS A  LIST OF OTHER TYPE OFELEMENTS-->
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

如果省略 &lt;ItemsControl.ItemTemplate&gt; 元素,则将 DataTemplate 添加到 ItemsControlItems 集合中,并且不能同时设置其 ItemsSource 属性。

尝试这样做会导致抛出 System.InvalidOperationException 异常,并显示您收到的错误消息。

多个 ItemsControl 绑定到同一个源集合是完全可以的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2012-03-03
    • 1970-01-01
    • 2011-10-12
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    相关资源
    最近更新 更多