observablecollection

Why does ObservableCollection<T> implement INotifyPropertyChanged?

懵懂的女人 提交于 2020-01-05 01:30:27
问题 In .NET 4.0, there isn't a single property defined by ObservableCollection<T> nor does it override any property of its parent or interfaces. So why does ObservableCollection<T> implement INotifyPropertyChanged ? One reason I can think of is that it makes it easier for subclasses to define their own properties and use the OnPropertyChanged method implemented by ObservableCollection<T> . But is this the main reason? 回答1: Of the properties Item , Items and Count , only Item actually has a setter

ArgumentOutOfRangeException when replacing items in an ObservableCollection<T>

感情迁移 提交于 2020-01-03 21:14:52
问题 I'm working on a Refresh() extension method for ObservableCollection which adds, removes or replaces items based on a matching key (this means when bound to a DataGrid the grid doesn't re-scroll and items don't change their position unless they were removed). Problem is when I replace items in the ObservableCollection the last item throws an ArgumentOutOfRangeException, what am I missing here? public static void Refresh<TItem, TKey>(this ObservableCollection<TItem> target, IEnumerable<TItem>

Observe PropertyChanged on items in an ObservableCollection using System.Reactive

ぐ巨炮叔叔 提交于 2020-01-02 18:04:45
问题 I have: public class Vm { private ObservableCollection<Thing> _things; public ObservableCollection<Thing> Things { get { return _things ?? (_things = new ObservableCollection<Thing>()); } } } And public class Thing :INotifyPropertyChanged { private string _value; public string Value { get { return _value; } set { if (value == _value) return; _value = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual

Observe PropertyChanged on items in an ObservableCollection using System.Reactive

随声附和 提交于 2020-01-02 18:04:41
问题 I have: public class Vm { private ObservableCollection<Thing> _things; public ObservableCollection<Thing> Things { get { return _things ?? (_things = new ObservableCollection<Thing>()); } } } And public class Thing :INotifyPropertyChanged { private string _value; public string Value { get { return _value; } set { if (value == _value) return; _value = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; [NotifyPropertyChangedInvocator] protected virtual

How do you apply multiple filter functions on one collection view source, one after another (AND relation)

断了今生、忘了曾经 提交于 2020-01-02 07:26:10
问题 I have been working with a Collection View Source object that is bound to a listview and the CVS is a view on top of Observable Collection of objects. I know how to apply a filter using the following technique: cvs.Filter += new FilterEventHandler(SomeFilterFunction); This works fine when you are only filtering in one function. The problem is when I want to filter on top of the already filtered CVS. If I have another function that filters the objects in the view based on different criteria,

Grouping data in WPF treeview

走远了吗. 提交于 2020-01-02 05:15:10
问题 I want to create a WPF TreeView with two grouping options (radio buttons). So the data will be grouped in two different ways in a 2-level hierarchy, the lowest level being the actual data items, and the groups being just a way to represent the data for easier understanding. They would also be able to select items by group (checkboxes) but I got that part already figured out, e.g. if I want to represent database objects and want to have them grouped either by schema or by object type (table,

Re-evaluate LINQ query when ObservableCollection changes

风流意气都作罢 提交于 2020-01-02 05:05:51
问题 I have a common issue that I'd like to (hopefully) find a better solution for moving forward. I have an ObservableCollection containing a master list of data. In my client code I need to 'transform' the data into a new form for display to the user. I use a LINQ statement like: var newList = (from item in observable select new { FirstInitial = item.Name[0] }); I know it's pretty rudimentary but it is enough to demonstrate the problem. (Notice the projection, this is not a simple filter or

Why ObservableCollection is not updated on items change?

折月煮酒 提交于 2020-01-02 03:29:10
问题 I noticed that ObservableCollection in WPF reflects changes in GUI only by adding or removing an item in the list, but not by editing it. That means that I have to write my custom class MyObservableCollection instead. What is the reason for this behaviour? Thanks 回答1: The ObservableCollection has no way of knowing if you make changes to the objects it contains - if you want to be notified when those objects change then you have to make those objects observable as well (for example by having

Implement AddRange on ObservableCollection with proper support for DataBinding

我的未来我决定 提交于 2020-01-01 17:28:11
问题 I would like my own descendant of ObservableCollection to support AddRange method. Here is what I currently have: public class ObservableCollectionPlus<T> : ObservableCollection<T> { public void InsertRange(IEnumerable<T> items) { this.CheckReentrancy(); foreach (var item in items) Items.Add(item); var type = NotifyCollectionChangedAction.Reset; var colChanged = new NotifyCollectionChangedEventArgs(type); var countChanged = new PropertyChangedEventArgs("Count"); OnPropertyChanged(countChanged

How do I execute a foreach lambda expression on ObservableCollection<T>?

牧云@^-^@ 提交于 2020-01-01 09:10:09
问题 How do I execute a foreach lambda expression on ObservableCollection<T>? There is not method of foreach with ObservableCollection<T> although this method exists with List<T>. Is there any extension method available? 回答1: There is no method available by default in the BCL but it's straight forward to write an extension method which has the same behavior (argument checking omitted for brevity) public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action) { foreach ( var cur in