为什么此XAML会出现错误:使用ItemsSource之前Items集合必须为空 [英] Why is this XAML getting the error: Items collection must be empty before using ItemsSource

查看:117
本文介绍了为什么此XAML会出现错误:使用ItemsSource之前Items集合必须为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以从这段代码中联想到为什么ItemsSource行将获得

Can anyone conjure from this code why the ItemsSource line would be getting a

项目集合之前必须为空 使用ItemsSource.

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

error? Most solutions I've found point to ill-composed XAML, e.g. an extra element etc. which I don't seem to have. When I take out

ItemsSource ="{绑定客户}"

ItemsSource="{Binding Customers}"

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

it runs without an error (but of course doesn't display my list of customers).

Customers是在ViewModel中定义的,因此其中包含3个CustomerViewModel:

Customers is defines thusly in the ViewModel and has 3 CustomerViewModels in it:

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"
    />

推荐答案

您正在尝试设置ItemsControl的ItemsSource,但是您已经有了子级.这两个中的哪个应适用?您放置在ItemsControl内的按钮还是作为ItemsSource放入其中的集合?该错误消息是完全合理的.

You are trying to set the ItemsSource of the ItemsControl but you already have children. Which of those two should apply? The Button you put inside the ItemsControl or the collection you are handing into it as ItemsSource? The error message is perfectly reasonable.

您将不得不从ItemsControl中删除按钮或删除ItemsSource属性.您不能同时插入项目和设置ItemsSource.

You would have to either remove the button from the ItemsControl or remove the ItemsSource attribute. You can't insert items and set ItemsSource at the same time.

这篇关于为什么此XAML会出现错误:使用ItemsSource之前Items集合必须为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆