Implementing ItemCreated in ViewModel (MVVM)

2 posts, 0 answers
  1. Kevin
    Kevin avatar
    13 posts
    Member since:
    Dec 2012

    Posted 08 Jun Link to this post

    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


  2. Nasko
    Nasko avatar
    555 posts

    Posted 13 Jun Link to this post

    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.

    Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
  3. DevCraft R3 2016 release webinar banner
Back to Top