WPF仅使用集合项目的子集来绑定到组合 [英] WPF Binding to a Combo using only a subset of a Collection's items
问题描述
我试图设置一个双向绑定到一个组合框,只使用一个集合的对象的选择。目前,一切正常,如果我只是想绑定在colelction的一切,但在下面的示例类,如果我只想显示项目,其中Active = True?我可以使用LINQ像ItemsSource = FROM x IN Coll WHERE x.Active = True过滤项目,但后来我失去了TwoWay绑定。即,如果来源中的名称或活动状态是从其他地方更新的,则组合框不会自动更新。
可以做吗?如果没有,是否有任何人必须处理这个问题有一些建议吗?
'类
公共类测试
实现ComponentModel.INotifyPropertyChanged
私有_Name作为字符串
私有_Active作为布尔
Public Sub New(名称为字符串,活动为布尔)
_Name = Name
_Active = Active
End Sub
公共属性名称()As String
结束类别
$ b b
'声明一个集合并添加一些测试,然后在页面加载中绑定到Cbo
Dim Coll As New ObservableCollection
Coll.Add(New Test(Test1,True))
Coll.Add(New Test(Test2,False))
Coll.Add(New Test(Test3,True))
TheComboBox.ItemsSource = Coll
解决方案两个选项:
您可以使用像 Bindable LINQ 这样的框架,使您的LINQ查询返回可观察的集合(因此绑定保持为二维,方式)。
或者,您可以将ComboBox的项目绑定到CollectionViewSource,并通过Filter事件处理程序运行每个项目:
< CollectionViewSource
Source ={Binding MyItems}
Filter =OnlyActiveItems
x:Key =ItemsView/&
< ComboBox ItemsSource ={Binding Source = {StaticResource ItemsView}}/>
代码隐藏:
private void OnlyActiveItems(object sender,FilterEventArgs e)
{
e.Accepted = false;
var item = e.Item as Text;
if(item == null)return;
e.Accepted = item.Active;
}
注意,我不完全确定CollectionViewSource会识别INotifyPropertyChanged接口并在一个元素更改时重新查询列表。我真的建议Bindable LINQ如果过滤器的方法不工作。I'm trying to set a TwoWay binding to a combo box using only a selection of a collection's objects. Currently, everything works out ok if I just want to bind everything in the colelction, but in the example class below, what if I only want to show items where Active=True? I can filter the items using LINQ like ItemsSource = FROM x IN Coll WHERE x.Active=True but then I lose the TwoWay binding. Ie, if the name or the active state in the source is updated from elsewhere, the combo box does not automatically update.
Is the possible to do? If not, does anyone who has had to deal with this have some suggestions?
'The Class Public Class Test Implements ComponentModel.INotifyPropertyChanged Private _Name As String Private _Active As Boolean Public Sub New(Name As String, Active As Boolean) _Name=Name _Active=Active End Sub Public Property Name() As String End Class 'Declare a Collection and add some Tests, then bind to Cbo in Page Load Dim Coll As New ObservableCollection Coll.Add(New Test("Test1", True)) Coll.Add(New Test("Test2", False)) Coll.Add(New Test("Test3", True)) TheComboBox.ItemsSource=Coll
解决方案Two options:
You can use a framework like Bindable LINQ that makes your LINQ queries return observable collections (thus the binding stays as two-way).
Or you could bind your ComboBox's items to a CollectionViewSource and run each item through a Filter event handler:
<CollectionViewSource Source="{Binding MyItems}" Filter="OnlyActiveItems" x:Key="ItemsView"/> <ComboBox ItemsSource="{Binding Source={StaticResource ItemsView}}" />
with code-behind:
private void OnlyActiveItems(object sender, FilterEventArgs e) { e.Accepted = false; var item = e.Item as Text; if (item == null) return; e.Accepted = item.Active; }
Note that I'm not entirely sure that a CollectionViewSource will recognise the INotifyPropertyChanged interface and re-query the list when one element changes. I'd really suggest Bindable LINQ if the filter approach doesn't work.
这篇关于WPF仅使用集合项目的子集来绑定到组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!