Click here to Skip to main content
15,947,785 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi there, I have a WPF application where I use ListCollectionView as ItemsSource of a ComboBox. This ListCollectionView mainly populate from an AudioDevice class. Actually, previously I make a List based on the AudioDevice class. My Listcollectionview working very much perfectly. But I want to detect if there any item changes happened on it at runtime of the application?

How can I do that? one important point to remember is, all the things I do from Codebehind. I don't use any MVVM base architecture in this scenario.

Here is the code snippet of the last part where I declare the ListCollectionView.

ListCollectionView lcv = new(devices); lcv.GroupDescriptions.Add(new PropertyGroupDescription("Direction")); cmb1.ItemsSource = lcv;


What I have tried:

I try to solve this problem by using
INotifyCollectionChanged
but that code not working and unable to detect changes at runtime.
Posted
Updated 6-Mar-23 23:43pm
Comments
Chris Copeland 7-Mar-23 4:42am    
Can I ask what "But I want to detect if there any item changes happened on it at runtime of the application" means? Do you mean changes to the device list which should propagate down to the combobox, or changes the user might make in the UI which should affect the device list?
Member 15061773 7-Mar-23 5:28am    
Thank you very much sir, for your reply, I want to mean "changes to the device list which should propagate down to the combobox"
PIEBALDconsult 7-Mar-23 10:31am    
It sounds like you want to use items which raise events for changes to themselves, not just a collection which raises events when items are added and removed from it.
You will probably need to define your own class which wraps the AudioDevice class.
Member 15061773 7-Mar-23 10:58am    
Thank you very much for your reply, I already asked this question with proper code in Microsoft question and answer forum.

Here is the link : https://learn.microsoft.com/en-us/answers/questions/1141456/how-to-refresh-combobox-items-at-runtime-when-comb

I can not understand what you saying by "You will probably need to define your own class which wraps the AudioDevice class". Any code guide will a great help.

The documentation is quite clear, it lists the events that are supported: ListCollectionView Class (System.Windows.Data) | Microsoft Learn[^].

In your case, you want to listen to the CollectionChanged[^] event.

UPDATE

Here is how to handle the CollectionChanged event:
C#
private void OnCollectionChanged(
    object? sender,
    NotifyCollectionChangedEventArgs e)
{
    // any new items?
    if (e.NewItems?.Count > 0)
    {
        // Item(s) added
    }

    // any to remove? ... not required for this purpose.
    if (e.OldItems?.Count > 0)
    {
        // Item(s) removed
    }
}
 
Share this answer
 
v2
Comments
Member 15061773 7-Mar-23 6:33am    
I already tried CollectionChanged but that does not detect any item changes. I wish you can give me any code example.
Graeme_Grant 7-Mar-23 7:13am    
It sure does. See my update on how to handle the event notifications.
I would say it depends on what type of collection the devices variable is. If it's just a List<> then changes won't be reflected in the UI because a List<> doesn't emit an event when it's content changes.

You probably have two options:

1) Whenever you get an updated devices list, you would have to update the combobox with this by creating a new ListCollectionView and assigning it to the cmb1.ItemsSource

2) Create an ObservableCollection<> and store it within the class. You could then create a ListCollectionView which uses this observable collection as the source, and then populate the observable collection with the list of devices. This would mean that any changes to the observable collection should also apply to the ListCollectionView and should apply to the UI when updated.

The second option can be trickier as it means adding, updating and/or removing entries in the observable collection to match what is in the devices list. It might come down to whichever option could be easiest for you.
 
Share this answer
 
Comments
Member 15061773 7-Mar-23 6:49am    
Your answer is outstanding, I already understand the theory part. In your first option you say, Whenever I get an updated devices list I need to update the ComboBox with a ListCollectionView and assigning it to the cmb1.ItemsSource.

Suppose, I open the ComboBox dropdown and at that time any of the item from the device list is unplugged or remove from the computer, How can I detect that?


I also understand your second option but I don't know how to apply it in code. I wish you can give me proper code example.
Chris Copeland 7-Mar-23 7:05am    
One way or another you're probably going to need to have some timer or background task to produce this list of devices, it's not clear in the code how this list is being created but I assume you're manually running some process to get it. If you're after a way to have events raised when devices are added/removed then you'd need to check the documentation on whatever it is you're using to produce the list.

I can't really provide full code but essentially whenever the device list is refreshed (either via a background task or through some events) you'll need to take the new device list, loop over it and add/update/remove items in the observable collection so that both collections match. As I say this will be more work than just updating the item source each time. But any changes to the observable collection will cascade to the ListCollectionView and automatically update the dropdown contents :)
Member 15061773 7-Mar-23 7:15am    
Again brilliant answer, I fully understand, ultimately what you say is option 2 is really better because observable collection will cascade to the ListCollectionView and automatically update the dropdown contents.

but any code guide on option 2? How can I declare an observable collection as itemsource for a ListCollectionView?

By the thank you very much for your crystal clear theory answer.
Chris Copeland 7-Mar-23 8:27am    
Here is an example .NET Fiddle[^] which gives a very, very rough idea of how it could work. The rest is up to you!
Member 15061773 7-Mar-23 8:53am    
Love the effort you put in to answer my question, but still I am unable to understand. I already asked this question with full code and proper explanation on Microsoft question and answer forum but I don't get any satisfied answer from there.

Here is the link : https://learn.microsoft.com/en-us/answers/questions/1141456/how-to-refresh-combobox-items-at-runtime-when-comb

Make sure that you read this question throughly, and give your decent code answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900