リストの再読み込みで選択したアイテムをバインドすることに関して、ComboBoxについて質問があります。
class Model : ViewModelBase
{
/// <summary>
/// The <see cref="Title" /> property's name.
/// </summary>
public const string TitlePropertyName = "Title";
private string _title = "";
/// <summary>
/// Sets and gets the Title property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string Title
{
get
{
return _title;
}
set
{
if (_title == value)
{
return;
}
RaisePropertyChanging(TitlePropertyName);
_title = value;
RaisePropertyChanged(TitlePropertyName);
}
}
/// <summary>
/// The <see cref="Id" /> property's name.
/// </summary>
public const string idPropertyName = "Id";
private int _myId = 0;
/// <summary>
/// Sets and gets the id property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public int Id
{
get
{
return _myId;
}
set
{
if (_myId == value)
{
return;
}
RaisePropertyChanging(idPropertyName);
_myId = value;
RaisePropertyChanged(idPropertyName);
}
}
}
バインドされたViewModel:
class MainViewModel : ViewModelBase
{
public MainViewModel()
{
PopulateCommand = new RelayCommand(() =>
{
Populate();
});
SetCommand = new RelayCommand(() =>
{
Id = 3;
});
}
public void Populate()
{
MyList = new ObservableCollection<Model>(){
new Model(){
Id = 1,
Title = "numer1"
},
new Model(){
Id = 2,
Title = "numer2"
},
new Model(){
Id = 3,
Title = "numer3"
},
new Model(){
Id = 4,
Title = "numer4"
},
};
}
public RelayCommand PopulateCommand { get; private set; }
public RelayCommand SetCommand { get; private set; }
/// <summary>
/// The <see cref="MyList" /> property's name.
/// </summary>
public const string MyListPropertyName = "MyList";
private ObservableCollection<Model> _myList = null;
/// <summary>
/// Sets and gets the MyList property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public ObservableCollection<Model> MyList
{
get
{
return _myList;
}
set
{
if (_myList == value)
{
return;
}
RaisePropertyChanging(MyListPropertyName);
_myList = value;
RaisePropertyChanged(MyListPropertyName);
}
}
/// <summary>
/// The <see cref="Id" /> property's name.
/// </summary>
public const string IdPropertyName = "Id";
private int _id = 0;
/// <summary>
/// Sets and gets the Id property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public int Id
{
get
{
return _id;
}
set
{
if (_id == value)
{
return;
}
RaisePropertyChanging(IdPropertyName);
_id = value;
RaisePropertyChanged(IdPropertyName);
}
}
}
xaml:
<ComboBox
ItemsSource="{Binding MyList}"
DisplayMemberPath="Title" SelectedValue="{Binding Id, Mode=TwoWay}" SelectedValuePath="Id" IsSynchronizedWithCurrentItem="True"/>
<Button Content="Populate"
Command="{Binding PopulateCommand}" />
<Button Content="Set"
Command="{Binding SetCommand}" />
<TextBlock Text="{Binding Id}" Width="100"/>
したがって、populateButtonはコンボボックスバインディングのリストを作成し、setButtonはIDを3に設定します。次に、リストに再度データを入力すると、コンボボックスでIdが1になります。まだ3である必要がありますか?つまり、コンボボックスでid=3のモデルの選択項目を設定するべきではありません。
また、IsSynchronizedWithCurrentItemをtrueに設定せずに試しました。次に、リストをリロードした後、コンボボックスで何も選択されていません。selectedItemをIdと同期する方法はありますか?
リストを最初に初期化してから、IDを設定して、コンボボックスが選択したアイテムを更新できるようにする必要があるようです。しかし、最初にsetButtonを使用してからデータを入力すると、期待どおりの結果が得られるのはなぜですか(選択したアイテムは3番目のアイテムです)(最初に使用した場合のみ)?