2009-04-22 3 views
11

を使用する前に、空である必要がありますのItemsSourceラインがなぜ、このXAMLは、エラーを取得している:ItemsコレクションはのItemsSource

Items collection must be empty before using ItemsSource.

エラーを取得されるだろう、なぜ誰もがこのコードから想起させることはできますか?私が見つけたほとんどのソリューションは、あまり作成されていないXAMLを指しています。私は持っていないような余分な要素など。私が取り出したとき

ItemsSource="{Binding Customers}"

エラーなしで実行されますが(ただし、私の顧客リストは表示されません)。ある

お客様はViewModelににthusly定義し、その中の3 CustomerViewModelsあります見する場所の

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> 

ANSWER:

私は確かにボタンがItemsControlに外にする必要があり、それを見落とし、不正な形式のXAMLを持っていた:

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

は、あなたがあなたのコントロールのXML要素内の任意のテキストを忘れてしまった場合、私は – stambikk

答えて

9

あなたがののItemsSourceを設定しようとしていますItemsControlが既に子を持っています。どちらの2つを適用する必要がありますか? ItemsControlの中に入れたボタン、またはItemsSourceとして渡しているコレクション?エラーメッセージは完全に合理的です。

ItemsControlからボタンを削除するか、ItemsSource属性を削除する必要があります。アイテムを挿入したり、ItemsSourceを同時に設定することはできません。

+1

同じエラーが発生します助け: '<コンボボックスのItemsSource =「{バインディングお客様が}」>これは'それはを失敗します。 –

2

あなたのItemsControlにはボタンがあります。 ItemsControlにはすでにアイテムがあるので、ItemsSourceプロパティを設定することはできません。

ボタンの宣言を</ItemsControl>終了タグの下に移動します。

関連する問題