ItemsSource を使用する前に、Items コレクションが空である必要があります。

表題のエラーについて意味が分からないので調べてみた。

条件としては

コンボボックス(ItemsControl継承パーツしてるやつ)などで

XAMLでも要素書いているにも関わらず

バインドでItemsSorceに値を設定する場合に発生する。

CombBoxData.ItemsSorce = combData;

CombBoxDataはデータクラスだ、中身はこんな感じ。

/// <summary>
/// データリストを取得または設定します。
/// </summary>
public ObservableCollection<IItemsDataObject> ItemsSource
{
    get
    {
        return this.DatasField;
    }
set
    {
        this.DatasField = value;
        this.OnPropertyChanged("ItemsSource");
    }
}

SourceReference Source
を確認、今回のエラーにかかわる部分を見つけた。

        // This puts the ItemCollection into ItemsSource mode.
        internal void SetItemsSource(IEnumerable value, Func<object, object> GetSourceItem = null)
        {
            // Allow this while refresh is deferred.
 
            // If we're switching from Normal mode, first make sure it's legal.
            if (!IsUsingItemsSource && (_internalView != null) && (_internalView.RawCount > 0))
            {
                throw new InvalidOperationException(SR.Get(SRID.CannotUseItemsSource));
            }
 
            _itemsSource = value;
            _isUsingItemsSource = true;


_internalViewにはXAMLで設定されている子要素が設定されている。

IsUsingItemsSourceは一度Bindingで設定するとtrueになるのだが、

今回は初期設定のためFalseのままエラー判定が有効になってしまい例外が発生する。

自分でクリアしてくれても良いのになー。と思わなくもないが

XAMLでも書かれてバインドもされている参照元が複数ある状態になってしまうので禁止しているのだろう。