我对您的情况知之甚少,但可以说您有以下数据模型来保存您的数据,实现 INotifyPropertyChangedInterface:
public class A : INotifyPropertyChanged
{
public String SomeProperty {....}
...
}
然后你有一个包含你的项目的视图模型,让它从 som 基类继承。我推荐 Galasoft MVVM Light 并进一步扩展您的需求。 OnPropertyChanges 调用 INotifyPropertyChanged。无论如何,我在这里偷工减料:
public YourViewModel : YourViewModelBase
{
private ObservableCollection<A> yourItems_ = new ObservableCollection<A>();
public ObservableCollection YourItems {
get { return yourItems_;}
set { if( yourItems_!=value ) {
_yourItems = value;
OnPropertyChanged();
}
return _yourItems;
}
}
}
然后在您的 xaml 中,您只需绑定到属性 YourItems 并在数据模型上设置显示成员。
<UserControl.....>
<UserControl.DataContext><vm:YourViewModel/></UserControl.DataContext>
<Grid>
<Grid.RowDefinition="Auto"/>
<Grid.RowDefinition="Auto"/>
<Grid.RowDefinition="*"/>
<Grid/>
<ListBox Grid.RowDefinition="0" ItemsSource="{Binding YourItems}" DisplayMemberPath="SomeProperty"/>
<ListBox Grid.RowDefinition="1" ItemsSource="{Binding YourItems}" DisplayMemberPath="SomeProperty"/>
</UserControl>
请注意,您可以编辑模板以获取项目的另一种可视化表示,使用 DisplayMamberPath 只会将其作为字符串添加到列表框表示中,除非您有一个覆盖 .ToString(); 的类;方法。
如果您需要过滤相同的项目来源,请查看 ICollectionView。
希望对你有帮助,
干杯,
斯蒂安