当我将ListBox直接绑定到ObservableCollection时,我得到了我的ListBox中显示的实时更新,但是只要我在混合中添加其他LINQ方法,我的ListBox就不会再通知ObservableCollection的任何更改.
在这里,让我举一个例子来说明;
public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<String> Words = new ObservableCollection<string>();
    public MainPage()
    {
        InitializeComponent();
        listBox1.ItemsSource = Words;
    }
    private void AddButton_Click(object sender, RoutedEventArgs e)
    {
        Words.Add(DateTime.Now.ToString());
    }
}
在这里,我向一个简单的页面添加了一个Button和一个ListBox,然后单击该按钮使新项目立即出现在ListBox中.
但是,如果我改变了
        listBox1.ItemsSource = Words;
至
        listBox1.ItemsSource = Words.Where(w => w.Contains(":"));
ListBox不再更新.
如何在我的ObservableCollection和ListBox之间添加"过滤器",并且仍然可以在不必再次设置.ItemsSource的情况下进行更新?
我有一个ObservableCollection,我需要引用一个特定的项目.如果该项目不存在,我需要通过Reactive Extensions监视它是否/何时出现项目,但在设置语句时需要一些帮助.我仍然不熟悉所有不同的Linq扩展如何工作,所以我不知道如何做到这一点.谁能指出我正确的方向?
为了更好地说明,我需要类似以下内容:
public class myitem :INotifyPropertyChanged
{
    private string _key;
    private string _value;
    public string key
    {
        get { return _key; }
        set { _key = value; NotifyPropertyChanged("key"); }
    }
    public string myvalue
    {
        //proper getter/setter, blah, blah
    }
}
ObservableCollection<myitem> _collection = mycollection;
var x = Observable.FromEvent<NotifyCollectionChangedEventHandler, NotifyCollectionChangedEventArgs>(
    h => new NotifyCollectionChangedEventHandler(h),
    h => _collection.CollectionChanged += h,
    h => _collection.CollectionChanged -= h);
string keywaitingfor = "thiskey";
string valuewaitingfor = x.Where(xx => xx.key == keywaitingfor).First().myvalue;
这不完全是我的情景,但希望你能看到我正在尝试做的事情.ObservableCollection可能不包含任何要开始的项目,并且属性值是异步的.我知道最后一行是不对的,我需要在lambda中的类PropertyChanged事件上有一个Observable ...但我仍然对如何在满足两个条件时获得该值等待感到困惑.
我刚开始使用MVVM并遇到了障碍,我希望有人可以帮助我.我正在尝试使用2个列表框创建一个简单的视图.第一个列表框中的选择将填充第二个列表框.我创建了一个类,用于存储我想要绑定的信息.
MyObject类(Observable Object只是一个实现INotifyPopertyChanged的基类)
public class MyObject : ObservableObject
{
    String _name = String.Empty;
    ObservableCollection<MyObject> _subcategories;
    public ObservableCollection<MyObject> SubCategories
    {
        get { return _subcategories; }
        set
        {
            _subcategories = value;
            RaisePropertyChanged("SubCategories");
        }
    }
    public String Name
    {
        get { return _name; }
        set
        {
            _name = value;
            RaisePropertyChanged("Name");
        }
    }
    public MyObject()
    {
        _subcategories = new ObservableCollection<EMSMenuItem>();
    }
}
在我的viewmodel中,我创建了两个ObservableCollections
public ObservableCollection<EMSMenuItem> Level1MenuItems { get; set; }
public ObservableCollection<EMSMenuItem> Level2MenuItems { get; set; }
在我的ViewModel的构造函数中,我有:
this.Level1MenuItems = new ObservableCollection<EMSMenuItem>();
this.Level2MenuItems = …我正在研究为什么在使用IList参数调用CollectionChanged时ObservableCollection/ListCollectionView/CollectionView引发NotSuportedException的原因.
//Throws an exception
private void collectionChanged_Removed(IList items)
{
    if (CollectionChanged != null)
        CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, items));
}
我找到了几个网页,谈论这个主题,他们建议使用Reset强制完全重绘UI的能力,或者只是简单地调用每个项目CollectionChanged或更具创造性的方式:http://geekswithblogs.net/NewThingsILearned/存档/ 2008/01/16/listcollectionviewcollectionview-犯规支持-notifycollectionchanged与-多items.aspx
我只是找不到为什么?对我来说,为什么会出现这种情况毫无意义.
我们在开发周期的某些时候都有这个缺乏功能的可能性,因为当你想快速添加多个项目时,Add方法只需要大量的开销,任何时候都会实现(.Net 5) ,C#6 ......).
编辑:
在我的具体情况下,我写了自己的课:
public class ObservableList<T> : IList<T>, IList, IEnumerable<T>,
    INotifyCollectionChanged
{
    public event NotifyCollectionChangedEventHandler CollectionChanged;
    //other stuff...
}
并仍然抛出所述NotSupportedException.
.net c# collections observablecollection inotifycollectionchanged
我有一个ObservableCollection<T>.我已将它绑定到ListBox控件,并且我已添加SortDescriptions到ListBox上的Items集合中,以使列表按我的方式排序.
当任何属性在子元素上发生更改时,我想在任何时候使用列表.
我所有的子元素都实现了INotifyPropertyChanged.
我怎么能投
from ObservableCollection<TabItem> into ObservableCollection<object>
这对我不起作用
(ObservableCollection<object>)myTabItemObservableCollection
我有一节课:
public class A : INotifyPropertyChanged
{
    public List<B> bList { get; set; } 
    public void AddB(B b)
    {
        bList.Add(b);
        NotifyPropertyChanged("bList");
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
和绑定(UserControl的DataContext是A的实例):
<ListBox ItemsSource="{Binding Path=bList}" />
显示元素,新对象添加到List后ListBox不会更新
将列表更改为ObservableCollection并删除NotifyPropertyChanged处理程序后,一切正常.
为什么列表不起作用?
在我的WPF应用程序中,我有一个XamDataGrid.网格绑定到ObservableCollection.我需要允许用户通过网格插入新行,但事实证明,为了使"添加新行"行可用,xamDataGrid的源需要实现IBindingList.ObservableCollection不实现该接口.
如果我将我的源代码更改为BindingList,它可以正常工作.但是,根据我在阅读这个主题时可以理解的,BindingList实际上是一个WinForms的东西,在WPF中并不完全支持.
如果我将所有ObservableCollections更改为BindingLists,我会犯错吗?有没有人有任何其他建议,我如何为我的xamDataGrid添加新的行功能,同时将源保持为ObservableCollection?我的理解是,有许多不同的网格需要实现IBindingList才能支持添加新的行功能,但我看到的大多数解决方案只是切换到BindingList.
谢谢.
是什么引起的潜在问题,ObservableCollection像支持操作AddRange或RemoveRange?由于ObservableCollection经常与WPF一起使用,因此必须有微软没有提供它们的原因.
您可以实现自己的集合,支持批量操作和实现INotifyCollectionChanged.如果我将这样的控件绑定到ItemsControl会发生什么?
有谁知道不支持批量更改的ItemsControls?
为什么ObservableCollection没有这样的RemoveAll方法List?
我已经实现了一个扩展方法来提供此功能ObservableCollection,但我想了解是否有特定原因不提供此功能.
由于Collection更改,它是否可能以某种方式影响数据绑定?这篇文章确实指出了一些在使用时可能出错的事情,但没有解决这个问题.ObservableCollections
c# ×9
wpf ×6
.net ×2
collections ×2
linq ×2
binding ×1
covariance ×1
data-binding ×1
infragistics ×1
listbox ×1
mvvm ×1
sorting ×1
xaml ×1