WPF ComboBox가 있고 MVVM을 사용하여 ItemsSource 및 SelectedItem 속성을 바인딩합니다. 기본적으로 사용자가 콤보 상자의 특정 항목을 선택하면 콤보 상자가 다른 항목을 선택합니다.WPF ComboBox SelectedItem binding
<ComboBox ItemsSource="{Binding TestComboItemsSource}" SelectedItem="{Binding TestComboItemsSourceSelected}"></ComboBox>
데모 용으로 SelectedItem을 업데이트하는 버튼이 있습니다.
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";
})); }
}
확인을, 그래서 콤보 상자는 사용자가 항목 "모두"를 선택 할 때마다 항목 "항목"을 선택하고 싶지 :
<Button Command="{Binding DoStuffCommand}">Do stuff</Button>
나는 나의 뷰 모델이 있습니다. 단추를 사용하여 콤보 상자의 SelectedItem을 업데이트 할 수 있으며 UI에 반영된 것을 볼 수 있습니다.
TestComboItemsSourceSelected 속성의 내 setter에서 viewModel을 업데이트하는 비슷한 논리가 있습니다. 사용자가 "all"을 선택한 경우 대신 SelectedItem을 "items"로 설정합니다. 코드별로, viewmodel 속성이 변경되지만 어떤 이유로이 속성이 UI에 반영되지 않습니다. 내가 놓친 게 있니? 내가 구현 한 방식의 부작용이 있습니까?
고마워요! 이런 생각이 나는 것 같아. – user832747