【发布时间】:2022-01-06 22:28:44
【问题描述】:
我有两个类 A 和 B,它们都实现了 IThingWithList 接口。
public interface IThingWithList
{
ObservableCollection<int> TheList;
}
A 中的列表包含 1、2、3 B 中的列表包含 4、5、6
我有一个控制器类,它有一个包含 A 和 B 的 IThingWithList 列表
public class MyControllerClass
{
public ObservableCollection<IThingWithList> Things { get; } = new ObservableCollection<IThingWithList>() { A, B };
public IThingWithList SelectedThing { get; set; }
}
现在,在 xaml 中,我有两个组合框,如下所示
<ComboBox
ItemsSource="{Binding MyController.Things}"
SelectedValue="{Binding MyController.SelectedThing, Mode=TwoWay}" />
<ComboBox
DataContext="{Binding MyController.SelectedThing}"
ItemsSource="{Binding TheList}" />
第一个 ComboBox 控制哪个(A 或 B)是第二个组合框的数据上下文。
问题:
当我从第一个 ComboBox 中选择 A 或 B 时,第二个 ComboBox 的列表项没有更新。
我尝试过的:
使 A 和 B 都成为 ObservableObjects
使 IThingWithList 实现 INotifyPropertyChanged
将 UpdateSourceTrigger 添加到 ItemsSource 绑定
搜索谷歌。
【问题讨论】:
-
您的绑定对象是否遵循 MVVM 模式?具体来说,您是否正在实施
INotifyPropertyChanged?通常我只是在 ItemSources Bound 列表(应该是 ObservableCollection)的 Setter 中执行OnPropertyChanged()。否则,您可能必须订阅 ObservableCollection。
标签: wpf combobox itemssource inotifycollectionchanged updatesourcetrigger