Set selected items in code behind

Hi,

I want to restore the selection in the code behind. However I do not get it working.
I use the combobox to filter a ListView and I want to set the SelectedMaterialList in code behind to the current filter, however this doesn't work.

Please see my code snippet below.

<combobox:SfComboBox  
                                            Grid.Row="1" Grid.ColumnSpan="3"
                                            Margin="8,0"
                                            DataSource="{Binding Materials}"
                                            ShowClearButton="True"
                                            ClearButtonColor="{DynamicResource PrimaryColor}"
                                            DisplayMemberPath="Name"                   
                                            MultiSelectMode="Token"
                                            SelectedItem="{Binding SelectedMaterialsList, Mode=TwoWay}"
                                            >
                                            <combobox:SfComboBox.TokenSettings>
                                                <combobox:TokenSettings 
                                                    FontSize="{OnIdiom Desktop=20, Tablet=20, Phone=14}" 
                                                    BackgroundColor="{DynamicResource Gray-100}" 
                                                    FontFamily="{StaticResource Montserrat-Regular}"
                                                    TextColor="{DynamicResource Gray-900}" 
                                                    SelectedBackgroundColor="{DynamicResource PrimaryColor}" 
                                                    IsCloseButtonVisible="true" 
                                                    CornerRadius="15" DeleteButtonPlacement="Right"
                                                    />
                                            </combobox:SfComboBox.TokenSettings>
                                        </combobox:SfComboBox>

private IList _SelectedMaterialsList = new ArrayList();
        public IList SelectedMaterialsList
        {
            get => _SelectedMaterialsList;
            set
            {
                if (Equals(value, _SelectedMaterialsList))
                    return;
                _SelectedMaterialsList = value;
                OnPropertyChanged();
            }
        }

Same works fine using WPF.

Thank you for your assistance. It's highly appreciated!

6 Replies 1 reply marked as answer

AR Andreas Reitberger August 5, 2020 04:14 AM UTC

Hi,
never mind, I figured it out.

First, I changed the Binding element for the "SelectedItem" to an ObservableCollection.

private ObservableCollection<Material3d> _selectedMaterials = new ObservableCollection<Material3d>();
        public ObservableCollection<Material3d> SelectedMaterials
        {
            get => _selectedMaterials;
            set
            {
                if (_selectedMaterials == value)
                    return;
                _selectedMaterials = value;
                OnPropertyChanged();
            }
        }

Then, the selection seemed to work, however the tokens were empty.
After that, I updated the NuGet packages to 18.0.2.48 and it was working.

Thanks,
Andreas

Marked as answer

SS Suganya Sethuraman Syncfusion Team August 5, 2020 05:50 AM UTC

Hi Andreas,

Thanks for the update.

Please get back to us if you need any further assistance.

Regards,
Suganya Sethuraman
  



MI Michael October 14, 2020 03:53 PM UTC

I'm trying to do the same, but receive an error when binding my selection

[0:] Binding: 'System.Collections.ObjectModel.ObservableCollection`1[System.Object]' can not be converted to type 'System.Collections.ObjectModel.ObservableCollection`1[BoxPlanner.Models.BackendDTO.ExerciseDTO]'.
The datasource and the SelectedItem are bound to identical types.

public ObservableCollection<ExerciseDTO> SelectedExercises { get; set; } = new ObservableCollection<ExerciseDTO>();
public ObservableCollection<ExerciseDTO> Exercises { get; set; } = new ObservableCollection<ExerciseDTO>();

<combobox:SfComboBox DataSource="{Binding Exercises}" 
                                 SelectedItem="{Binding SelectedExercises, Mode=TwoWay}"
                                 DisplayMemberPath="Name" 
                                 NoResultsFoundText="Nothing Found" 
                                 HeightRequest="40" x:Name="exerciseComboBox" 
                                 ValueChanged="ExerciseComboBox_ValueChanged"
                                 MultiSelectMode="Token" TokensWrapMode="Wrap"
                                 MaximumDropDownHeight="200" 
                                 Watermark="Search Exercise..." IsEditableMode="true"/>

I'm using the latest xamarin forms and sf 18.2.0.54
No idea what I'm doing different then the original poster


AR Andreas Reitberger October 14, 2020 04:01 PM UTC

Hi,
I can remember that you need to use "object" as datatype for your collection.
public ObservableCollection<object> SelectedExercises { get; set; } = new ObservableCollection<object>();

this should do the trick, at least it did for me. You later need to Cast() your objects back to the original type.
I don't know why syncfusion doesn't support other types than "objects" for the selected items...

Best,
Andreas


SS Suganya Sethuraman Syncfusion Team October 15, 2020 12:14 PM UTC

Hi Michael,

Greetings from Syncfusion.

We have tried to replicate the reported issue at our end, we are afraid that we are not able to reproduce the issue at our end.

We have checked the issue with as per your provided information, Please have the sample from the following link,

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxSelectedItem1987578156

Could you please check the issue with our sample and let us know  whether the issue reproduced also in our sample or not. If the issue was not reproduced please modify the sample with reported issue. It will help us to provide better solution at the earliest.

Regards,
Suganya Sethuraman.
 



SS Suganya Sethuraman Syncfusion Team October 15, 2020 12:15 PM UTC

Hi Andreas,

Currently, we don’t have direct support for your requirement. So, we have already logged this as a feature request, and you can track the status of this feature implement through below link

Feedback link: https://www.syncfusion.com/feedback/17705/provide-the-support-for-selecteditem-as-ienumerableobject-datatype-in

Please cast your vote to make it count. We will prioritize the features every release based on the demands and we do not have an immediate plan to implement this feature since we committed with already planned work. So, this feature will be available in any of our upcoming releases.

If you have any more specifications/suggestions to the feature request, you can add it as a comment in the portal.

Regards,
Suganya Sethuraman.
 


Loader.
Up arrow icon