定義済みの ComboBoxItem を、すでに ItemsSource プロパティが設定されている ComboBox に追加しようとしています。例:
(Select item)
Item 1
Item 2
Item 3
元のアイテム コレクションを変更せずにこれを行うことは可能ですか?
定義済みの ComboBoxItem を、すでに ItemsSource プロパティが設定されている ComboBox に追加しようとしています。例:
(Select item)
Item 1
Item 2
Item 3
元のアイテム コレクションを変更せずにこれを行うことは可能ですか?
CompositeCollection の使用法を示す MSDN のサンプル コードを次に示します。
<ComboBox>
<ComboBox.ItemsSource>
<CompositeCollection>
<ListBoxItem>Please Select</ListBoxItem>
<CollectionContainer Collection="{Binding Source={StaticResource YOURDATASOURCE}}" />
</CompositeCollection>
</ComboBox.ItemsSource>
</ComboBox>
CompositeCollection の使用方法を示す参考資料を次に示します。
1- http://msdn.microsoft.com/en-us/library/system.windows.data.compositecollection.aspx
2- http://robertbouillon.com/2010/04/17/adding-items-to-a-data-bound-wpf-combobox/
3-バインドされた CompositeCollection を使用するように ComboBox を変換するにはどうすればよいですか?
項目ソースのコンテンツを動的に変更する場合は、代わりに ObservableCollection を使用して、Add() メソッドにアクセスできるようにします。
private ObservableCollection<string> myStrings;
public MyClass()
{
myStrings = new ObservableCollection<string>();
myControl.ItemsSource = myStrings;
}
private void AddNewItem(string item)
{
myStrings.Add(item);
}