Declined
Last Updated: 02 Nov 2021 11:03 by ADMIN
Robert
Created on: 27 Oct 2021 09:30
Category: ComboBox
Type: Bug Report
0
Combobox SelectedItems (SelectionMode=Multiple) binding issue

Hello,

Let's say you have a view... You select multiple items in the combobox... Then you navigate away from that view, return, and you cannot bind the Combobox to previously selected items.

Well, you kind of can, but you can't. The combobox will happily display tokens, but it's just for display. They are not really selected.

Select some items:

(navigate away and back, bind SelectedItems to list above)

 

At this point, doing anything with these items will overwrite whatever was "selected".

By the way, this isn't visible in the screenshot above, but if you have a placeholder set up on the Combobox (I set it to an empty string), it will be shown on the right of the tokens. Which means the control really thinks it's empty.

 

TL;DR - cannot programatically set SelectedItems. The control's multiple selection mode is "one way to source", thus not usable in real world scenarios for editing data.

4 comments
ADMIN
Yana
Posted on: 02 Nov 2021 11:03

Hello Robert,

I've attached a sample app which shows how you can preselect items in RadComboBox by adding them to the SelectedItems collection - items are shown as tokens and are also selected in the dropdown.

Please download the attachment and give it a try.

Regards,
Yana
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.

Attached Files:
Robert
Posted on: 29 Oct 2021 11:40

Hi Yana,

I am unable to run your SDK browser (too many dependencies on things I don't have installed), but just by looking at the code of the Combo+Checkbox example, I don't think it deals with the problem I described.

My initial bug report wasn't quite right, but wasn't wrong either :)

I don't want to replace the whole collection. The problem is that unless you fire SelectAllCommand from codebehind (and that's the only way this works), whatever you added to SelectedItems will be destroyed the moment you try to interact with the control.

Let's say you have 10 items in your ItemsSource.

Add all 10 of those programatically to SelectedItems. They are visible as tokens. Everything is fine.

Open the dropdown.

Nothing is selected.

Try to select anything from the dropdown and the entire SelectedItems gets overwritten by whatever single item you clicked on.

If you don't interact with the control, SelectedItems is untouched. But if you do, it gets blown up.

So, the *only* way to make this work is as follows. Let's assume you have a list of 5 items and 2 need to be selected.

1) Add those 2 to ItemsSource

2) Fire SelectAllCommand from codebehind; the control will internally populate its internal selection list and SelectedItems

3) Add the remaining 3 items to ItemsSource

There's just no other way, CheckBox examples or not.

ADMIN
Yana
Posted on: 29 Oct 2021 08:42

Hello Robert,

Thank you for explaining the use case in details.

In general, RadComboBox provides read-only SelectedItems collection - this means that you can bind to the collection with one way binding ( you cannot replace it with new collection), still, you can add and remove items from it. 

Please take a look at the example here: ComboBox with CheckBox where the approach is demonstrated - the SelectedItems property of the ComboBox is bound to the ObservableCollection<object>  property and new items are added/removed from that collection from the ViewModel. In this way you can preselect items from the ViewModel constructor.

I hope I was of help. Let me know if you have any additional questions on this.

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Robert
Posted on: 27 Oct 2021 10:52

I have found a workaround in codebehind, but it's not very pretty in an MVVM app.

Basically, to make this work, the itemsSource for the combobox needs to be set to the selected items, then the SelectAll command should be invoked for the control to get itself together, then the non-selected tags inserted at index directly into ItemsSource.

 

if (_viewModel.Parameter.Tags.Count > 0)
{
    rcbTags.ItemsSource = _viewModel.Parameter.Tags;
    rcbTags.SelectAllCommand.Execute(null);
 
    for(var n = 0; n < _viewModel.TagsList.Count; n++)
    {
        if (!_viewModel.Parameter.Tags.Any(x => x.ID == _viewModel.TagsList[n].ID))
            (rcbTags.ItemsSource as List<Tag>).Insert(n, _viewModel.TagsList[n]);
    }
}