Bind data source to ObservableCollection<int>

Hi,

I've been struggling with this control to make it bind to an Observable Collection of ints. Basically, I want to use this control as a picker where the DataSource is a list of int values, and bind the selectedItem to an int in a different class.

The idea would be to have the following:

public MyClass : INotifyPropertyChanged
{
     private int _Value;
     public int Value
     {
          get => _Value;
          set
          {
               _Value = value;
               OnPropertyChanged();
          }
     }
}

public ComboBoxViewModel
{
     public MyClass myClass {get; set;} = new MyClass();

     public ObservableCollection<int> Options {get; set;} = new ObservableCollection<int>(){ 2, 4, 8, 16};
}

XAML:

<SfComboBox:SfComboBox DataSource = "{Binding Options}"
                                                 SelectedItem = "{Binding myClass.Value, Mode=TwoWay}" />      

I am not even getting the list of Options in my combobox.

Thanks!

3 Replies 1 reply marked as answer

RB Rabhia Beham Kathar Mideenar Syncfusion Team August 25, 2020 10:10 AM UTC

Hi Miguel,

Greetings from Syncfusion.

We have prepared a sample to achieve your requirement using SfComboBox control. Please use the below sample,

Sample:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxSample-617851581

Please check with the sample and let us know if you have any concerns.

Regards,
Rabhia Beham K.



MR Miguel Rubio August 26, 2020 08:40 AM UTC

Thanks Rabhia,

didn't expect the DataSource only accepting string lists. 

Moreover, if I add a watermark to the Combo Box, is it possible to re-show the watermark (doing something in codebehind) after it disappeared due to the selection of one of the items?

Basically what I want is that if the ComboBox IsEnabled property changes to False, to restore the watermark on the ComboBox. 

Thanks,

Miguel


RB Rabhia Beham Kathar Mideenar Syncfusion Team August 27, 2020 09:22 AM UTC

Hi Miguel,

We would like to let you know that the Watermark is only shown when the text is empty in the SfComboBox. It will get disappeared when we select any from the suggestion or set the SelectedItem from the ViewModel. Please check with the below link for reference,

UG Link: https://help.syncfusion.com/xamarin/combobox/customizing-combobox#watermark

Please let us know if you have any concerns.

Regards,
Rabhia Beham K.

Marked as answer
Loader.
Up arrow icon