- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 C# 中有一个 WPF 应用程序,其中有一个 MyCollection
类的对象延伸 ObservableCollection<MyType>
它包含用于将它们绑定(bind)到多个组合框的项目。
但是,每个 ComboBox 都必须显示此集合的一个子集(基于其元素的特定属性),并且该子集可能会根据用户输入而改变。
我如何才能获得这种行为,使每个子集都使用原始集合中的数据进行更新?这种情况是否有一些众所周知的设计模式?
编辑:由于我对这个问题的表述很容易被误解,这里有一个例子。我有一个 ObservableCollection<Person>
对象,其中类 Person
有一个 Age
和 Name
特性。我有三个组合框,前两个必须显示 Name
的 Person
奇数 Age
的对象,而第三个必须是偶数 Age
.他们的角色可能会在运行时发生变化(例如,第一个和最后一个必须显示奇数年龄,第二个必须显示偶数年龄)如果Person
对象被添加到集合或从集合中删除,更改必须反射(reflect)在相应的 ComboBoxes 上。 Name
和 Age
属性可以被认为是常量。
最佳答案
如果我正确理解你的问题,你需要某种过滤机制。
看看 ICollectionView
接口(interface)及其实现,例如 CollectionViewSource
,它们可能会帮助您实现这一目标。
您需要处理实现过滤逻辑的Filter
事件。
这是 MSDN 上的类(class) ( http://msdn.microsoft.com/en-us/library/system.windows.data.collectionviewsource(v=vs.110).aspx )
一个例子:
容器类:
public string Name { get; set; }
public string Capital { get; set; }
public Country(string name, string capital) {
this.Name = name;
this.Capital = capital;
}
模型类:
private ObservableCollection<Country> _countries;
private ICollectionView _european;
private ICollectionView _american;
public ObservableCollection<Country> Countries {
get {
if (_countries == null) {
_countries = new ObservableCollection<Country>();
}
return _countries;
}
}
public ICollectionView European {
get {
if (_european == null) {
_european = new CollectionViewSource {
Source = this.Countries
}.View;
_european.Filter += (e) => {
Country c = e as Country;
if (c.Name == "UK" || c.Name == "Ireland" || c.Name == "France") {
return true;
}
return false;
};
}
return _european;
}
}
public ICollectionView American {
get {
if (_american == null) {
_american = new CollectionViewSource {
Source = this.Countries
}.View;
_american.Filter += (e) => {
Country c = e as Country;
if (c.Name == "USA" || c.Name == "Canada" || c.Name == "Mexico") {
return true;
}
return false;
};
}
return _american;
}
}
初始化代码:
private Model _model;
public Model Model {
get {
if (_model == null) {
_model = new Model();
}
return _model;
}
}
public MainWindow() {
InitializeComponent();
this.DataContext = this.Model;
this.Model.Countries.Add(new Country("UK", "London"));
this.Model.Countries.Add(new Country("Ireland", "Dublin"));
this.Model.Countries.Add(new Country("France", "Paris"));
this.Model.Countries.Add(new Country("USA", "Washington D. C."));
this.Model.Countries.Add(new Country("Mexico", "Mexico City"));
this.Model.Countries.Add(new Country("Canada", "Ottawa"));
}
XAML:
<StackPanel>
<ComboBox
ItemsSource='{Binding Path=European}'>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel
Orientation='Horizontal'>
<TextBlock
Text='{Binding Path=Name}' />
<TextBlock
Text=', ' />
<TextBlock
Text='{Binding Path=Capital}' />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
<ComboBox
ItemsSource='{Binding Path=American}'>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel
Orientation='Horizontal'>
<TextBlock
Text='{Binding Path=Name}' />
<TextBlock
Text=', ' />
<TextBlock
Text='{Binding Path=Capital}' />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
关于c# - 将 ComboBox 绑定(bind)到 ObservableCollection 的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22339150/
这是一个有趣的案例,我无法在网上找到任何信息。我正在尝试创建一个网格,需要将 ObservableCollection 的 ObservableCollection 绑定(bind)到它。想象这样一个
如何复制ObservableCollection项目到另一个 ObservableCollection没有引用第一个集合?这里ObservableCollection影响两个集合的项目值更改。 代码
public class Alpha { public ObservableCollection Items { get; set; } public Alpha() {
我只是想知道如何拥有父集合的子集合? 例如, 我已经有一个 ObservableCollection 产品,它正在添加并正确绑定(bind)到 XAML。但是,现在我需要另一个包含产品项目的 Obse
我对 Silverlight 体验相对较新,我正在尝试创建一个带有 DomainService 的 MVVM 应用程序,该应用程序将 POCO 作为模型返回。我有一个 UserControl,它有一个
查看 Microsoft 站点上的 Windows 运行时引用,我能找到的唯一相关集合是 IObservableVector 。 .NET Projection ObservableCollectio
我正在尝试获取值“thisValueIwant”。有没有可能如此容易地获得这个值(value)?或者也许这两个 ObservableCollection 有另一种解决方案 public class F
我有一个 ObserveableCollection,其中包含另一个 ObserveableCollection。在我的 WPF 中,我设置了对 Persons.Lectures 的绑定(bind)。
我有一个包含 20 个项目(图像)和按钮(“下一个”)的 observablecollection。我如何获得像 observablecollection.next() 和 observablecol
我有一个 ObservableCollection . T 有一个 ToString() 方法。我想做的是转换 ObservableCollection至 ObservableCollection .
我有一个 DataGrid,它绑定(bind)到 ViewModel 中的一个 ObservableCollection。这是一个搜索结果DataGrid。问题是,在我更新搜索结果 Observabl
有一堆ObservableCollection Result并要求将它们全部组合成另一个 ObservableCollection AllResults所以我可以在 listview 中显示它. 只是
哪个是保存我的数据的更好解决方案,还是取决于某些条件? 示例情况 1: 您需要显示一个数据列表,选择后可以在新窗口中修改。 示例情况 2: 您需要显示可以在此列表中修改的数据列表。 最佳答案 当您使用
从 xml 中执行 ViewModel 的最佳方法是什么:
所以我有一个 BaseClass 以及继承自基类的几个子类 ChildClass1 ChildClass2 我有ObservableCollections需要就地排序的子类,我无法创建新的 Obser
我为 ObservableCollection 构建了一个简单的扩展方法 AddRange: using System; using System.Collections.Generic; using
我的情况是,我有“tablegenerateModel”类的 ObservableCollection,该类进一步包含“column_Data”类的 ObservableCollection,并且这个
我将有一个 7 个小“购物 list ”,然后是一个包含 7 个小 list 中所有项目的大 list 。 是否可以使用 databind 和 observablecollection,以便从小列表中
当所述 ObservableCollection 从 View 模型公开时,我无法找到正确的绑定(bind)语法来绑定(bind) ObservableCollection 中包含的项目的属性。 当我
我有以下类(class),效果很好 public class RemoteSource { ObservableCollection remote; string[] _servers
我是一名优秀的程序员,十分优秀!