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 776469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:27:13+00:00 2026-05-14T19:27:13+00:00

Is there a simple way to create a BindingList wrapper (with projection), which would

  • 0

Is there a simple way to create a BindingList wrapper (with projection), which would update as the original list updates?

For example, let’s say I have a mutable list of numbers, and I want to represent them as hex strings in a ComboBox. Using this wrapper I could do something like this:

BindingList<int> numbers = data.GetNumbers();
comboBox.DataSource = Project(numbers, i => string.Format("{0:x}", i));

I could wrap the list into a new BindingList, handle all source events, update the list and fire these events again, but I feel that there is a simpler way already.

  • 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-14T19:27:14+00:00Added an answer on May 14, 2026 at 7:27 pm

    I have just stumbled across this question and I realized I might post the code I ended up with.

    Since I wanted a quick solution, I made a sort of a poor man’s implementation. It works as a wrapper around an existing source list, but it creates a full projected list of items and updates it as needed. At first I hoped I could do the projection on the fly, as the items are accessed, but that would require implementing the entire IBindingList interface from scratch.

    What is does: any updates to the source list will also update the target list, so bound controls will be properly updated.

    What it does not do: it does not update the source list when the target list changes. That would require an inverted projection function and I didn’t need that functionality anyway. So items must always be added, changed or removed in the source list.

    Usage example follows. let’s say we have a list of numbers, but we want to display their squared values in a data grid:

    // simple list of numbers
    List<int> numbers = new List<int>(new[] { 1, 2, 3, 4, 5 });
    
    // wrap it in a binding list
    BindingList<int> sourceList = new BindingList<int>(numbers);
    
    // project each item to a squared item
    BindingList<int> squaredList = new ProjectedBindingList<int, int>
        (sourceList, i => i*i);
    
    // whenever the source list is changed, target list will change
    sourceList.Add(6);
    Debug.Assert(squaredList[5] == 36);
    

    And here is the source code:

    public class ProjectedBindingList<Tsrc, Tdest> 
        : BindingList<Tdest>
    {
        private readonly BindingList<Tsrc> _src;
        private readonly Func<Tsrc, Tdest> _projection;
    
        public ProjectedBindingList(
            BindingList<Tsrc> source, 
            Func<Tsrc, Tdest> projection)
        {
            _projection = projection;
            _src = source;
            RecreateList();
            _src.ListChanged += new ListChangedEventHandler(_src_ListChanged);
        }
    
        private void RecreateList()
        {
            RaiseListChangedEvents = false;
            Clear();
    
            foreach (Tsrc item in _src)
                this.Add(_projection(item));
    
            RaiseListChangedEvents = true;
        }
    
        void _src_ListChanged(object sender, ListChangedEventArgs e)
        {
            switch (e.ListChangedType)
            {
                case ListChangedType.ItemAdded:
                    this.InsertItem(e.NewIndex, Proj(e.NewIndex));
                    break;
    
                case ListChangedType.ItemChanged:
                    this.Items[e.NewIndex] = Proj(e.NewIndex);
                    break;
    
                case ListChangedType.ItemDeleted:
                    this.RemoveAt(e.NewIndex);
                    break;
    
                case ListChangedType.ItemMoved:
                    Tdest movedItem = this[e.OldIndex];
                    this.RemoveAt(e.OldIndex);
                    this.InsertItem(e.NewIndex, movedItem);
                    break;
    
                case ListChangedType.Reset:
                    // regenerate list
                    RecreateList();
                    OnListChanged(e);
                    break;
    
                default:
                    OnListChanged(e);
                    break;
            }
        }
    
        Tdest Proj(int index)
        {
            return _projection(_src[index]);
        }
    }
    

    I hope someone will find this useful.

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

Sidebar

Related Questions

Is there a simple way to create a selectable NSRect in Cocoa? In need
Is there a simple way to create a copy of a database or schema
Is there a simple way to create a link that does either? Basically, I
Is there a simple way to create a low priority transfer of a file?
Is there a simple way to create a zip archive in C++? I'm writing
Using Rx, is there a simple way to create a single Notification<T> ? The
Simply put, is there a way to create a 2D javascript array using similar
Is there a simple way of getting a HTML textarea and an input type=text
Is there a simple way to cache MySQL queries in PHP or failing that,
Is there a simple way in .NET to quickly get the current protocol, host,

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.