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

Multiselect Binding SelectedItems

10 Answers 2090 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Alex
Top achievements
Rank 1
Alex asked on 24 Oct 2016, 02:28 PM

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

 

10 Answers, 1 is accepted

Sort by
0
Nasko
Telerik team
answered on 25 Oct 2016, 08:40 AM
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!
0
Alex
Top achievements
Rank 1
answered on 25 Oct 2016, 09:44 AM

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

0
Nasko
Telerik team
answered on 28 Oct 2016, 06:55 AM
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!
0
Rama
Top achievements
Rank 1
answered on 12 Oct 2017, 06:21 PM

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

0
Dinko | Tech Support Engineer
Telerik team
answered on 17 Oct 2017, 12:26 PM
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.
0
Dan
Top achievements
Rank 1
answered on 05 Jul 2018, 06:33 PM

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?

0
Dan
Top achievements
Rank 1
answered on 05 Jul 2018, 07:18 PM
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).
0
Dinko | Tech Support Engineer
Telerik team
answered on 09 Jul 2018, 11:22 AM
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.
0
billy
Top achievements
Rank 2
Iron
Iron
Iron
answered on 08 Apr 2021, 02:19 AM

I have a behavior that does indeed populate the Collection of SelectedOptions on my ViewModel, BUT what I need to do is call an event when they are updated as I have another list that is built from the selections in my MultiSelect RadComboBox. How can I capture the change to the SelectedOptions Collection as it is of type ObservableCollection without PropertySet capabilty and therefore never raises the property changed event on the property.

Is there a way to capture each event when an Option is added to the collection?

1
Dinko | Tech Support Engineer
Telerik team
answered on 09 Apr 2021, 10:48 AM

Hi Billy,

I think the SelectionChanged event of the control is what you are looking for, but still, I am not sure. Can you try it and let me know if it fits in your case.

Regards,
Dinko
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ComboBox
Asked by
Alex
Top achievements
Rank 1
Answers by
Nasko
Telerik team
Alex
Top achievements
Rank 1
Rama
Top achievements
Rank 1
Dinko | Tech Support Engineer
Telerik team
Dan
Top achievements
Rank 1
billy
Top achievements
Rank 2
Iron
Iron
Iron
Share this question
or