This is a migrated thread and some comments may be shown as answers.

Implementing ItemCreated in ViewModel (MVVM)

1 Answer 22 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 08 Jun 2016, 02:17 PM

Hello All,
    I have a need to implement the ItemCreated event over in the ViewModel for a RadCombobox.  I need an example of binding the event to a function.  Does anyone know of an example out there???  In my View I have a RadComboBox that is bound to a List<string> in my ViewModel.  In the contructor of the ViewModel, I populate this list.  I need to handle the case when the user adds a new value to this ComboBox.  I saw in the UI for Web docs a mention of the ItemCreated event.  I think this is what 'm looking for, but don't have a clue as to how to go about implementing it in the WPF MVVM code.  Any clues will be greatly appreciated!!!

Thanks in advance,

Kevin Orcutt
Senior Software Engineer

Wurth Electronics ICS, Inc.
7496 Webster St., Dayton, OH 45414
Tel: 937.415.7700 
Toll Free: 877.690.2207
Fax: 937.415.7710
Email: Kevin.Orcutt@we-ics.com
http://www.we-ics.com
 
 

 

1 Answer, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 13 Jun 2016, 07:33 AM
Hello Kevin,

What we could suggest you for your scenario is instead of using List to use ObservableCollection. The List is a collection that does not implement the INotifyCollectionChanged interface. Thus there are any notifications when an item is added or removed from the collection and you will not be able to observe any changes when new item is added. The ObservableCollection implements the INotifyCollectionChanged interface and the UI will be automatically updated if any changes are made on the collection.

If you change List with ObservableCollection you will also be able to handle the CollectionChanged event of the ObservableCollection. You could handle that event inside your ViewModel without any concerns that the patter will break. The event will be fired every time a new item gets added to the ComboBox ItemsSource and will be similar to the desired ItemCreated event.

We have created a sample project that demonstrates the described above approach and you could run and evaluate it.

Hope this helps.


Regards,
Nasko
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
Tags
ComboBox
Asked by
Kevin
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Share this question
or