Multiselect Binding SelectedItems

9 posts, 0 answers
  1. Alex
    Alex avatar
    25 posts
    Member since:
    Nov 2010

    Posted 24 Oct 2016 Link to this post

    Hello

    How can I bind the selected Items from the Combobox when using "AllowMultipleSelection"?

    ItemTemplate: <CheckBox Content="{Binding Name}" IsChecked="{Binding RelativeSource={RelativeSource AncestorType=telerik:RadComboBoxItem}, Path=IsSelected}" />

    Sincerly

    A.Kasar

     

  2. Nasko
    Admin
    Nasko avatar
    724 posts

    Posted 25 Oct 2016 Link to this post

    Hello Ates,

    SelectedItems is read only property inherited from MultiSelector class, that is why it cannot be bound to a property in the ViewModel.

    What we could suggest you as one possible appraoch is to extend the functionality of RadComboBox using an attached property and add the desired items to the SelectedItems collection of RadComboBox. More detailed information how to extend the functionality you could find here:
    http://www.telerik.com/blogs/extending-the-functionality-of-radcontrols-with-attached-properties

    Attached you could find a sample that demonstrate the described in the blog post approach.

    Hope this helps.

    Regards,
    Nasko
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  3. Alex
    Alex avatar
    25 posts
    Member since:
    Nov 2010

    Posted 25 Oct 2016 Link to this post

    Hello

    thank you for your answer.

    I have tested your sample. The binded property SelectedAgencies does not update at runtime. It always contains the original / initial items. What do I have to change / add in order to have the updated response from the control? How does it get passed on in runtime to the SelectedAgencies correctly?

    Sincerly

    A.Kasar

  4. Nasko
    Admin
    Nasko avatar
    724 posts

    Posted 28 Oct 2016 Link to this post

    Hi Ates,

    The proposed in my previous response approach is just a simple one and you might need to continue improve it in order to make everything work as expected.

    So, in order your changes in the selection get notified to the collection itself you could use behavior instead of an attached property. Inside it you need to attach to the CollectionChanged event of the collection you are bound to and to the SelectionChanged event of the ComboBox. When selection is made the SelectionChanged event will be fired and you could update the collection you are bound to with the new item that is selected from RadComboBox.

    Please, check the attached sample that demonstrates that approach.

    We hope this will help you.

    Regards,
    Nasko
    Telerik by Progress
    Do you need help with upgrading your WPF project? Try the Telerik API Analyzer and share your thoughts!
  5. Rama
    Rama avatar
    1 posts
    Member since:
    Jun 2012

    Posted 12 Oct 2017 in reply to Nasko Link to this post

    Hi Nasko,

    I verified this code and wanted to use this in my application.

    The SelectedAgencies in the ViewModel is not updated with the selection of multiple items in the combo box.

    Can you please verify and update the code.

    Thanks

    Rama

  6. Dinko
    Admin
    Dinko avatar
    844 posts

    Posted 17 Oct 2017 Link to this post

    Hello Rama,

    I have examined the project and when you select items from the combo box the SelectedAgencies collection is populated correctly. I have modified the project to demonstrated that the collection is populated. You can find the project attached to this reply. When you run the project you can observe at the top a TextBlock which is bind to the Count property of the collection. Also in the view model, I have subscribed to the CollectionChanged. You can place a breakpoint in the event handler to see that the event is called.

    If I am in the wrong direction, can you elaborate more on what is not working on your side?

    Regards,
    Dinko
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
  7. Dan
    Dan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 05 Jul 2018 Link to this post

    Dinko,

    I am looking for something just like this, however after trying your behavior code, you still cannot set the SelectedItems property on the RadComboBox.  When I try to do this I always get a -1 result, meaning it was not able to add the item to the collection.  Is this as designed or is there another way to set the binding?

  8. Dan
    Dan avatar
    4 posts
    Member since:
    Apr 2017

    Posted 05 Jul 2018 in reply to Dan Link to this post

    When I say you cannot set the SelectedItems property, you cannot set the binding after initial load.  For example, I'm using a multiselect combo box to save sets of settings.  When the user selects a set of settings to view what options were saved for that set, the multiselect combo boxes will not show what options were selected in that set.  Even using the other example above by Nasko produces the same results (sets it on initialize, but after that, you cannot set it again).
  9. Dinko
    Admin
    Dinko avatar
    844 posts

    Posted 09 Jul 2018 Link to this post

    Hi Dan,

    I am not sure that I have fully understood your scenario. I am assuming that you are adding the custom Behavior runtime. I have tested this in the attached project and was able to set the binding runtime. Check the modified version of my project. When you run the project you can observe that the TextBlock is not updated while you are selecting items from the drop-down. Select a few items and press the button. In the Click event handler, I am adding the SelectedItemsBehavior to the Behaviors collection of the RadComboBox. Now when you select items the collection from the view model is updated. 

    Can you take a look at this project and let me know if it works on your side. If not, can you elaborate more on your case? How are you setting the custom behavior? 

    Regards,
    Dinko
    Progress Telerik
    Want to extend the target reach of your WPF applications, leveraging iOS, Android, and UWP? Try UI for Xamarin, a suite of polished and feature-rich components for the Xamarin framework, which allow you to write beautiful native mobile apps using a single shared C# codebase.
Back to Top