observablecollection

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

旧城冷巷雨未停 提交于 2020-01-01 09:10:03
问题 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

Metro App CollectionViewSource ObservableCollection Filter

吃可爱长大的小学妹 提交于 2020-01-01 06:41:47
问题 It appears that filtering an ObservableCollection with CollectionViewSource is not possible in WinRT : See here! I can filter using LINQ , but how do I get the UI to update if changes that affect the filtered data are made? 回答1: I ended up writing my own class to achieve the desired effect: public class ObservableCollectionView<T> : ObservableCollection<T> { private ObservableCollection<T> _view; private Predicate<T> _filter; public ObservableCollectionView(IComparer<T> comparer) : base

WPF: OnCollectionChanged not firing

Deadly 提交于 2019-12-31 04:50:13
问题 Using VS 2102, .NET 4.0 and MVVM Light. I have the following code that reads items from an XML file into an ObservableCollection. I then want to update the Save button with a Converter if the collection changes (using an IsDirty flag) but the OnCodeCollectionChanged method is not being executed. The collection is properly displayed in the data grid and I can add new entries to the data grid but the OnCodeCollectionChanged method is never called. I am NOT trying to catch the change of an

How to Display ObservableCollection<string> in a UserControl

末鹿安然 提交于 2019-12-30 11:12:43
问题 I'm new to WPF and I've found some similar questions but can't quite figure out the last part. I have a ViewModel with an ObservableCollection that contains error messages. I want to display these on the form AND allow the user to select and copy all or part of the messages. (In the past in WinForm apps I used a RichTextBox for this, but I can't figure out how to bind to one to the collection in WPF.) I got the look I was after with the following xaml, but there is no built-in way to select

Replace Entire ObservableCollection with another ObservableCollection

最后都变了- 提交于 2019-12-30 06:27:26
问题 public class Alpha { public ObservableCollection<Beta> Items { get; set; } public Alpha() { Items = new ObservableCollection<Beta>(); } public void DoSomething() { Items = GetNewItems(); // whenever I do this, Items gets a new referene, // so every WPF binding (e.g. datagrids) are broken } public ObservableCollection<Beta> GetNewItems() { var ret = new ObservableCollection<Beta>(); // some logic for getting some items from somewhere, and populating ret return ret; } } How can I replace the

Can I filter a collection from xaml?

强颜欢笑 提交于 2019-12-30 04:35:35
问题 I have a wpf-mvvm application. I have an observable collection in my viewmodel public ObservableCollection<BatchImportResultMessageDto> ImportMessageList { get; set; } "BatchImportResultMessageDto" contains two properties.. result type..and message. Result type can be success or failure. I need to display success in one list box ..and failure in another listbox. I can do this..by having 2 observable collections in viewmodel to hold success/failure. public ObservableCollection

WPF ObservableCollection<T> vs BindingList<T>

一曲冷凌霜 提交于 2019-12-30 04:06:10
问题 In my WPF app I have a XamDataGrid. The grid is bound to an ObservableCollection. I need to allow users to insert new rows through the grid but it turns out that in order for the "Add New Row" row to be available, the xamDataGrid's source needs to implement IBindingList. ObservableCollection does not implement that interface. If I change my source to a BindingList, it works ok. However, from what I can understand from reading up on this topic, BindingList is really a WinForms thing and is not

What's the best way to update an ObservableCollection from another thread?

元气小坏坏 提交于 2019-12-29 11:46:10
问题 I am using the BackgroundWorker to update an ObservableCollection but it gives this error: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." What's the best and most elegant way to solve this, with the least amount of work. I don't want to write low level lock-based multi-threading code. I have seen some solutions online but they are several years old, so not sure what the latest consensus is for the solution of

What's the best way to update an ObservableCollection from another thread?

允我心安 提交于 2019-12-29 11:46:10
问题 I am using the BackgroundWorker to update an ObservableCollection but it gives this error: "This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread." What's the best and most elegant way to solve this, with the least amount of work. I don't want to write low level lock-based multi-threading code. I have seen some solutions online but they are several years old, so not sure what the latest consensus is for the solution of

WPF: Xaml, create an observable collection<object> in xaml in .NET 4.0

夙愿已清 提交于 2019-12-29 07:35:32
问题 the web site says you can in .NET 4.0 I cant seem to do it though, what assesmbly references and xmlns' do i need the following does not work xmlns:coll="clr-namespace:System.Collections.ObjectModel;assembly=mscorlib" <coll:ObservableCollection x:TypeArguments="x:Object"> <MenuItem Command="ApplicationCommands.Cut"/> <MenuItem Command="ApplicationCommands.Copy"/> <MenuItem Command="ApplicationCommands.Paste"/> </coll:ObservableCollection> 回答1: ObservableCollection<T> is defined in the System