Displaying ObservableCollection Items

ALawrence007 used Ask the Experts™
Hi to all,

I am running into a small issue in my current project. I am creating a small WPF shopping cart application. In this application I created an observable collection of my store Items. When the user clicks to add an item it adds that item to the observable collection of my shopping cart.... How do I display that to the user if there are more than one of an item?

Currently that item shows up multiple times and I don't want that. I want to display the item once with a quantity of (x).

How would I display that to the user?

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Interesting problem. I would have approached it a different way. Upon trying to insert a new item, I'd have checked to see if it's already in the collection and if so, prevented the duplicate insertion. They could then edit the original item if desired.

If you truly want to allow for multiple, identical entries in the collection, then I believe you'd want to bind the UI to a proxy layer, such as a LINQ query that rolls up dupes into individual items. You'd then need to regenerate that view each time the collection were modified. Seems a bit messy.


"Upon trying to insert a new item, I'd have checked to see if it's already in the collection and if so, prevented the duplicate insertion"
Is exactly what I wanted to do, but don't know how to. Could you maybe supply me with some sample code in order to be able to do what you mentioned above?
Oh boy. That's much easier said than done. I've never coded a Wpf application that didn't use the model/view/viewmodel paradigm. This means I have a UI layer, a model layer (domain objects) and a view model layer, which is where all the real work takes place.

In my last project, I make use of the IEditableCollectionView interface to add new items into the UI listbox. When the user saves, there's a hook in there to perform the DB update. If that fails for any reason, including unique constraints, I put the user right back into edit mode after displaying the message.

I have no idea what the structure of your application is. You'd need to be really specific in exactly what you need, such as "I have a method that's called when the user hits Save. The data the user entered is stored in xxxxx. They entered it via a form -or- they entered it directly into the list control. The collection that the list is bound to is xxxx." and so on. I couldn't even guess where to begin.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.


OK, Let me try to explain. I am a beginner when it comes to this and ask for your patience.
I have a SQL database with all my items found in my store. I created a Items Class with Itemcode, description price and so on using VB. Using Linq I run my query against the database to "get" my items. These Items I load into a new observablecollection(of storeItems), (the class I mentioned above). I then set my list views data source to that observablecollection. Not being very well versed in WPF I created a gridview that is bound to my listviews selected item (reason is that I wanted to allow the user to click add in the listview, but I don't know how to find out if they clicked the button add or if they just selected that item in the listview)  and then the gridview I have an add button where I allow the user to add items to their shopping bag.
The shopping bad is also a class I created using VB that looks exactly like the store Items I mentioned above, but with a quantity field extra.
How can I check if there is already an item with that code in my shopping bags observablecollection and then modify that number? Do I do a indexof search?
Hope this will explain a little more of what is going on in my application.
Let me know if you need more.
The ObservableCollection has a method called Contains(), which lets you check if it has an item already.

If you're storing custom classes in it, you'll need to override the Equals member in your class, as shown below, so that the OC will know what constitutes a equality.
public override bool Equals(object obj)
    return (obj.MyPrimaryKeyProperty == this.MyPrimaryKeyProperty);

Open in new window


It took me a little while, but got there in the end.

Thanks Todd

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial