【问题标题】:Why is this XAML getting the error: Items collection must be empty before using ItemsSource为什么此 XAML 出现错误:使用 ItemsSource 之前,Items 集合必须为空
【发布时间】:2009-04-22 11:32:31
【问题描述】:

任何人都可以从这段代码中想到为什么 ItemsSource 行会得到一个

Items 集合之前必须为空 使用 ItemsSource。

错误?我发现的大多数解决方案都指向不正确的 XAML,例如我似乎没有的额外元素等。当我拿出来时

ItemsSource="{绑定客户}"

它运行时没有错误(但当然不会显示我的客户列表)。

Customers 在 ViewModel 中如此定义,其中包含 3 个 CustomerViewModel:

Customer[] customers = Customer.GetCustomers();
IEnumerable<CustomerViewModel> customersViewModels = customers.Select(c => new CustomerViewModel(c));
this.Customers = new ReadOnlyCollection<CustomerViewModel>(customersViewModels.ToArray());

有什么推荐的地方吗?

<UserControl x:Class="TestCommandSink123.View.CustomersView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestCommandSink123"
    xmlns:view="clr-namespace:TestCommandSink123.View"
    xmlns:vm="clr-namespace:TestCommandSink123.ViewModel"
    xmlns:sink="clr-namespace:TestCommandSink123.CommandSinkClasses"
    sink:CommandSinkBinding.CommandSink="{Binding}"
    >

    <UserControl.CommandBindings>
        <sink:CommandSinkBinding Command="vm:CustomersViewModel.CloseAllCustomersCommand"/>
    </UserControl.CommandBindings>

    <DockPanel>
        <ItemsControl
            DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <view:CustomerView/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
            <Button
                Command="vm:CustomersViewModel.CloseAllCustomersCommand"
                Content="Close All"
                Margin="0,0,0,8"
                />
        </ItemsControl>

    </DockPanel>
</UserControl>

回答:

我确实有格式错误的 XAML,只是忽略了它,按钮应该在 ItemsControl 之外

<ItemsControl
    DockPanel.Dock="Bottom" ItemsSource="{Binding Customers}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <view:CustomerView/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<Button
    Command="vm:CustomersViewModel.CloseAllCustomersCommand"
    Content="Close All"
    Margin="0,0,0,8"
    />

【问题讨论】:

  • 只是提到格式错误的 XAML 对我有帮助

标签: c# wpf mvvm


【解决方案1】:

您正在尝试设置 ItemsControl 的 ItemsSource,但您已经有了孩子。这两个应该申请哪一个?您放在 ItemsControl 中的 Button 还是您作为 ItemsSource 交给它的集合?错误信息是完全合理的。

您必须从 ItemsControl 中删除按钮或删除 ItemsSource 属性。您不能同时插入项目和设置 ItemsSource。

【讨论】:

  • 如果您忘记了控件的 XML 元素中的任何文本,也会出现同样的错误:&lt;ComboBox ItemsSource="{Binding Customers}"&gt;This makes it fail&lt;/ComboBox&gt;
【解决方案2】:

您的 ItemsControl 中有一个 Button。由于 ItemsControl 中已经有一个项目,它不允许您设置它的 ItemsSource 属性。

将 Button 声明移到 &lt;/ItemsControl&gt; 结束标记下。

【讨论】:

    【解决方案3】:

    你看过这个问题吗?这似乎是您问题的答案。

    "Items collection must be empty before using ItemsSource."

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-19
      • 2016-03-23
      • 2014-06-03
      • 2012-03-04
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多