1

他のサブコレクションに加えて、単一のアイテムを CompositeCollection (ComboBox のソースとして使用) に追加する方法はありますか?

  • 項目のオブジェクト インスタンスは ViewModel のプロパティです
  • 追加されたアイテムと他のサブコレクションのアイテムの両方が同じタイプです。

ここに私が持っているものがあります:

<ComboBox x:Name="combo">
    <ComboBox.Resources>
        <CollectionViewSource x:Key="CollectionAsAProperty" Source="{Binding CollectionA, Mode=TwoWay}" SelectedItem="{Binding CurrentItem}" />
    </ComboBox.Resources>
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding Source={x:Static local:MyViewModel.StaticCollection}}" />
            <CollectionContainer Collection="{Binding Source={StaticResource CollectionAsAProperty}}"/>

   ===> what should I add here to add another item of DataContext.AnotherItem

        </CompositeCollection>
    </ComboBox.ItemsSource>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding Name}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

編集:それは多少働いた

<ObjectDataProvider ObjectInstance="{x:Static local:MyViewModel.AnotherItem}"/>

ただし、(1)静的プロパティが必要であり、(2)添付されたコマンド(簡潔にするためにここには示されていません)では、返される選択されたアイテムのタイプはObjectDataProviderのタイプではなくMyViewModel.AnotherItemであり、ViewModel に問題を引き起こします。

4

1 に答える 1

0

複合コレクションをビューモデルに移動し、コード ビハインドでAdd メソッドを使用してすべての異なるコレクションを追加します。

    var myAnimals = new List<Animal>() { new Animal() { Name = "Zebra" },
                                         new Animal() { Name = "Cobra" }} ;

    var myPeople = new List<Person>() { new Person() { Name = "Snake Pliskin" },
                                        new Person()  { Name = "OmegaMan" }};


    var FavoriteThings = new CompositeCollection();

    FavoriteThings.Add(myAnimals);
    FavoriteThings.Add(myPeople);

public class Animal
{
    public string Name { get; set; }
}


public class Person
{
    public string Name { get; set; }
}
于 2013-10-27T15:19:32.087 に答える