Quelqu'un peut-il conjure de ce code pourquoi la ligne ItemsSource serait d'obtenir unPourquoi est-ce XAML obtenir l'erreur: la collecte des objets doit être vide avant d'utiliser ItemsSource
erreurItems collection must be empty before using ItemsSource.
? La plupart des solutions que j'ai trouvées pointent vers XAML mal-composé, par ex. un élément supplémentaire, etc., que je ne semble pas avoir. Quand je sors
ItemsSource="{Binding Customers}"
il fonctionne sans erreur (mais bien sûr n'affiche pas ma liste de clients).
clients est définit thusly dans le ViewModel et a 3 CustomerViewModels en elle:
Customer[] customers = Customer.GetCustomers();
IEnumerable<CustomerViewModel> customersViewModels = customers.Select(c => new CustomerViewModel(c));
this.Customers = new ReadOnlyCollection<CustomerViewModel>(customersViewModels.ToArray());
toute suggestion d'où chercher?
<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>
RÉPONSE:
J'ai bien ai malformé XAML, juste a oublié, le bouton doit être en dehors du 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"
/>
vient de mentionner XAML malformé m'a aidé – stambikk