Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3480810
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:23:16+00:00 2026-05-18T10:23:16+00:00

When using WPF databinding, I obviously can’t do something along the lines of MyCollection

  • 0

When using WPF databinding, I obviously can’t do something along the lines of MyCollection = new CollectionType<Whatever>( WhateverQuery() ); since the bindings have a reference to the old collection. My workaround so far has been MyCollection.Clear(); followed by a foreach doing MyCollection.Add(item); – which is pretty bad for both performance and aesthetics.

ICollectionView, although pretty neat, doesn’t solve the problem either since it’s SourceCollection property is read-only; bummer, since that would have been a nice and easy solution.

How are other people handling this problem? It should be mentioned that I’m doing MVVM and thus can’t rummage through individual controls bindings. I suppose I could make a wrapper around ObservableCollection sporting a ReplaceSourceCollection() method, but before going that route I’d like to know if there’s some other best practice.

EDIT:

For WinForms, I would bind controls against a BindingSource, allowing me to simply update it’s DataSource property and call the ResetBindings() method – presto, underlying collection efficiently changed. I would have expected WPF databinding to support a similar scenario out of the box?

Example (pseudo-ish) code: WPF control (ListBox, DataGrid, whatever you fancy) is bound to the Users property. I realize that collections should be read-only to avoid the problems demonstrated by ReloadUsersBad(), but then the bad code for this example obviously wouldn’t compile 🙂

public class UserEditorViewModel
{
    public ObservableCollection<UserViewModel> Users { get; set; }

    public IEnumerable<UserViewModel> LoadUsersFromWhateverSource() { /* ... */ }

    public void ReloadUsersBad()
    {
        // bad: the collection is updated, but the WPF control is bound to the old reference.
        Users = new ObservableCollection<User>( LoadUsersFromWhateverSource() );
    }

    public void ReloadUsersWorksButIsInefficient()
    {
        // works: collection object is kept, and items are replaced; inefficient, though.
        Users.Clear();
        foreach(var user in LoadUsersFromWhateverSource())
            Users.Add(user);
    }

    // ...whatever other stuff.
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:23:17+00:00Added an answer on May 18, 2026 at 10:23 am

    If the object MyCollection is of implements INotifyPropertyChanged, you can simply replace the collection.

    An example:

    public class MyClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<Whatever> _myCollection;
    
        private void NotifyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        public ObservableCollection<Whatever> MyCollection
        {
            get
            {
                return _myCollection;
            }
            set
            {
                if (!ReferenceEquals(_myCollection, value))
                {
                    _myCollection = value;
                    NotifyChanged("MyCollection");
                }
            }
        }
    }
    

    With this, when you assign a collection, WPF detects this and everything gets updated.

    This is how I’d solve this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do you Clear/Remove DataBinding in Silverlight? similar to: Remove binding in WPF using
Using WPF .NET 4.0 in VS2010 RTM: I can't create a fullscreen WPF popup.
I'm building simple dictionary application using WPF. I'm using MVVM pattern, databinding and FlowDocument
A part of the editor i'm writing uses a Wpf-TreeView. I'm using DataBinding and
I am studying WPF databinding using this tutorial . Here is my XAML: Window
I have a WPF window with a textbox, using standard WPF Databinding to an
I am using EF4 with WPF. I am databinding to the DataGrid in a
Using WPF I have a list of rectangles (which can have an undefined number
i wonder if it is possible to cascade converters when using wpf databinding. e.g.
I would like to hide a progressbar in WPF using databinding. Whenever a property

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.