¿Alguien puede evocar de este código por qué la línea ItemsSource sería conseguir un error dePor qué es esto XAML consiguiendo el error: colección de artículos debe estar vacío antes de usar ItemsSource
Items collection must be empty before using ItemsSource.
? La mayoría de las soluciones que he encontrado apuntan a XAML mal compuesto, p. un elemento extra, etc. que no parezco tener. Cuando saco
ItemsSource="{Binding Customers}"
funciona sin un error (pero, por supuesto, no muestra mi lista de clientes).
clientes es thusly define en el modelo de vista y tiene 3 CustomerViewModels en ella:
Customer[] customers = Customer.GetCustomers();
IEnumerable<CustomerViewModel> customersViewModels = customers.Select(c => new CustomerViewModel(c));
this.Customers = new ReadOnlyCollection<CustomerViewModel>(customersViewModels.ToArray());
Cualquier sugerencia de dónde buscar?
<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>
RESPUESTA:
que tienen efectivamente XAML mal formado, simplemente se pasa por alto que, El botón debe estar fuera del 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"
/>
acaba de mencionar XAML malformación me ayudó a – stambikk