私は WPF ComboBox を持っており、MVVM を使用して ItemsSource プロパティと SelectedItem プロパティをバインドしています。基本的に私がやりたいことは、ユーザーがコンボボックスで特定のアイテムを選択すると、代わりにコンボボックスが別のアイテムを選択することです。
<ComboBox ItemsSource="{Binding TestComboItemsSource}" SelectedItem="{Binding TestComboItemsSourceSelected}"></ComboBox>
デモ用に、SelectedItem を更新するボタンもあります。
<Button Command="{Binding DoStuffCommand}">Do stuff</Button>
私は私のビューモデルにこれを持っています:
public ObservableCollection<string> TestComboItemsSource { get; private set; }
public MyConstructor()
{
TestComboItemsSource = new ObservableCollection<string>(new []{ "items", "all", "umbrella", "watch", "coat" });
}
private string _testComboItemsSourceSelected;
public string TestComboItemsSourceSelected
{
get { return _testComboItemsSourceSelected; }
set
{
if (value == "all")
{
TestComboItemsSourceSelected = "items";
return;
}
_testComboItemsSourceSelected = value;
PropertyChanged(this, new PropertyChangedEventArgs(TestComboItemsSourceSelected))
}
}
private ICommand _doStuffCommand;
public ICommand DoStuffCommand
{
get
{
return _doStuffCommand ?? (_doStuffCommand = new RelayCommand(p =>
{
TestComboItemsSourceSelected = "items";
})); }
}
わかりましたので、ユーザーがアイテム「all」を選択するたびに、ComboBox にアイテム「items」を選択させたいと思います。ボタンを使用して、コンボボックスの SelectedItem を更新でき、これが UI に反映されていることがわかります
TestComboItemsSourceSelected プロパティのセッターで viewModel を更新する同様のロジックがあります。ユーザーが "all" を選択した場合は、代わりに SelectedItem を "items" に設定します。コード的には、viewmodel プロパティが変更されますが、これは何らかの理由で UI に反映されません。何か不足していますか?私がこれを実装した方法の何らかの副作用はありますか?