gpt4 book ai didi

c# - 将 ComboBox 绑定(bind)到 ObservableCollection 的一部分

转载 作者:行者123 更新时间:2023-11-30 12:28:18 24 4
gpt4 key购买 nike

我在 C# 中有一个 WPF 应用程序,其中有一个 MyCollection 类的对象延伸 ObservableCollection<MyType>它包含用于将它们绑定(bind)到多个组合框的项目。

但是,每个 ComboBox 都必须显示此集合的一个子集(基于其元素的特定属性),并且该子集可能会根据用户输入而改变。

我如何才能获得这种行为,使每个子集都使用原始集合中的数据进行更新?这种情况是否有一些众所周知的设计模式?


编辑:由于我对这个问题的表述很容易被误解,这里有一个例子。我有一个 ObservableCollection<Person>对象,其中类 Person有一个 AgeName特性。我有三个组合框,前两个必须显示 NamePerson奇数 Age 的对象,而第三个必须是偶数 Age .他们的角色可能会在运行时发生变化(例如,第一个和最后一个必须显示奇数年龄,第二个必须显示偶数年龄)如果Person对象被添加到集合或从集合中删除,更改必须反射(reflect)在相应的 ComboBoxes 上。 NameAge属性可以被认为是常量。

最佳答案

如果我正确理解你的问题,你需要某种过滤机制。

看看 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com