WPF绑定到一个组合只使用一个集合的项目的一个子集 [英] WPF Binding to a Combo using only a subset of a Collection's items

查看:113
本文介绍了WPF绑定到一个组合只使用一个集合的项目的一个子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图设置一个双向绑定只使用一个选择的集合的对象的组合框。目前,一切正常出好的如果我只是想在colelction绑定的一切,但在下面的示例类,如果我只想要显示的项目,其中活动=真?我可以过滤喜欢的ItemsSource =使用LINQ项目从X IN科尔WHERE x.Active = TRUE,但后来我失去双向绑定。也就是说,如果名称或源中的活动状态是从其他地方进行更新,组合框不会自动更新。

是可能的吗?如果没有,有没有人谁不得不处理这个有一些建议?

 '的Class
公共类测试
    实现ComponentModel.INotifyPropertyChanged    私人_Name作为字符串
    私人_active由于布尔    公共子新(名称作为字符串,积极作为布尔)
        _name =名称
        _active =活动
    结束小组    公共属性Name()作为字符串
末级'声明集合,并添加一些测试,然后绑定到CBO在页面加载
昏暗科尔作为新的ObservableCollection
Coll.Add(新测试(测试1,真))
Coll.Add(新测试(Test2的假))
Coll.Add(新测试(Test3的真))
TheComboBox.ItemsSource =科尔


解决方案

有两种选择:

您可以像使用可绑定LINQ 一个框架,使您LINQ查询返回可观察到的集合(因此绑定保持为双向)。

或者你可以在你的组合框的项目绑定到CollectionViewSource,并通过一个过滤器的事件处理程序上运行的每个项目:

 < CollectionViewSource
    来源={结合MyItems}
    过滤器=OnlyActiveItems
    X:键=ItemsView/><组合框的ItemsSource ={绑定源= {StaticResource的ItemsView}}/>

与code-背后:

 私人无效OnlyActiveItems(对象发件人,FilterEventArgs E)
{
    e.Accepted = FALSE;    VAR项目= e.Item为文本;
    如果(项目== NULL)回报;    e.Accepted = item.Active;
}

请注意,我不是完全确定一个CollectionViewSource将承认INotifyPropertyChanged接口,并重新查询列表时,一个元素的变化。我真的建议可绑定的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆