C# 为什么此 XAML 收到错误消息:在使用 ItemsSource 之前,Items 集合必须为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/776766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is this XAML getting the error: Items collection must be empty before using ItemsSource
提问by Edward Tanguay
Can anyone conjure from this code why the ItemsSource line would be getting a
任何人都可以从这段代码中想到为什么 ItemsSource 行会得到一个
Items collection must be empty beforeusing ItemsSource.
在使用 ItemsSource之前,Items 集合必须为空。
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
错误?我发现的大多数解决方案都指向组合不良的 XAML,例如我似乎没有的额外元素等。当我拿出
ItemsSource="{Binding Customers}"
ItemsSource="{绑定客户}"
it runs without an error (but of course doesn't display my list of customers).
它运行没有错误(但当然不会显示我的客户列表)。
Customers is defines thusly in the ViewModel and has 3 CustomerViewModels in it:
客户因此在 ViewModel 中定义,并在其中包含 3 个 CustomerViewModel:
Customer[] customers = Customer.GetCustomers();
IEnumerable<CustomerViewModel> customersViewModels = customers.Select(c => new CustomerViewModel(c));
this.Customers = new ReadOnlyCollection<CustomerViewModel>(customersViewModels.ToArray());
Any suggestions of where to look?
关于在哪里看的任何建议?
<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>
ANSWER:
回答:
I did indeed have malformed XAML, just overlooked it, the Button should be outside the ItemsControl:
我确实有格式错误的 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"
/>
采纳答案by Joey
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,但您已经有了孩子。这两个应该适用哪一个?您放在 ItemsControl 中的 Button 或您作为 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.
您必须从 ItemsControl 中删除按钮或删除 ItemsSource 属性。您不能同时插入项目和设置 ItemsSource。
回答by Matt Hamilton
Your ItemsControl has a Button in it. Since there's already an item in the ItemsControl, it's not letting you set its ItemsSource property.
您的 ItemsControl 中有一个 Button。由于 ItemsControl 中已经有一个项目,它不允许您设置其 ItemsSource 属性。
Move the Button declaration down under the </ItemsControl>
closing tag.
将 Button 声明向下移动到</ItemsControl>
结束标记下。
回答by Razzie
Have you looked at this question? It seems like the answer to your problem.
你看过这个问题吗?这似乎是您问题的答案。