Category / Section
How to avoid Empty DropDown displayed in SfComboBox
Empty collection is displayed in SfComboBox for the following reasons.
- Collection is not provided to the DataSource.
- DisplayMemberPath is not set to the corresponding item to be displayed in an item.
- Collection provided is not optional to the DataSource.
Collection is not provided to the DataSource
PProvide the collection to the DataSource that will not result in empty drop-down being displayed.
DisplayMemberPath iis s not set to the corresponding item to be displayed in an item.
DisplayMemberPath property, specifies the property path with type of filtering is done on business objects.
Code snippet [XAML]
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:combobox="clr-namespace:Syncfusion.XForms.ComboBox;assembly=Syncfusion.SfComboBox.XForms"
xmlns:local="clr-namespace:NamespaceName"
x:Class="NamespaceName.ClassName">
<ContentPage.BindingContext>
<local:EmployeeViewModel/>
</ContentPage.BindingContext>
<StackLayout VerticalOptions="Start" HorizontalOptions="Start" Padding="30">
<combobox:SfComboBox HeightRequest="40" x:Name="comboBox" DataSource="{Binding EmployeeCollection}" DisplayMemberPath="Name" />
</StackLayout>
</ContentPage>
Code snippet [C#]
public class Employee
{
private int id;
public int ID
{
get { return id; }
set { id = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
}
public class EmployeeViewModel : INotifyPropertyChanged
{
private ObservableCollection<Employee> employeeCollection;
public event PropertyChangedEventHandler PropertyChanged;
public ObservableCollection<Employee> EmployeeCollection
{
get { return employeeCollection; }
set { employeeCollection = value; }
}
private void RaisePropertyChanged(String name)
{
if (PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public EmployeeViewModel()
{
employeeCollection = new ObservableCollection<Employee>();
employeeCollection.Add(new Employee() { ID = 1, Name = "Frank" });
employeeCollection.Add(new Employee() { ID = 2, Name = "James" });
employeeCollection.Add(new Employee() { ID = 3, Name = "Steve" });
employeeCollection.Add(new Employee() { ID = 4, Name = "Lucas" });
employeeCollection.Add(new Employee() { ID = 5, Name = "Mark" });
employeeCollection.Add(new Employee() { ID = 6, Name = "Michael" });
employeeCollection.Add(new Employee() { ID = 7, Name = "Aldrin" });
employeeCollection.Add(new Employee() { ID = 8, Name = "Jack" });
employeeCollection.Add(new Employee() { ID = 9, Name = "Howard" });
}
Collection provided is not optional
SfComboBox DataSource type is "IEnumerable<Object>". Hence, provide the List<object> type source to avoid the empty collection being displayed from the List<double> type.
Code snippet [C#]
public MainPage()
{
InitializeComponent();
StackLayout layout = new StackLayout()
{
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Padding = new Thickness(30)
};
List<object> resolutionList = new List<object>();
resolutionList.Add("1.2");
resolutionList.Add("1.3");
resolutionList.Add("1.4");
resolutionList.Add("1.5");
resolutionList.Add("1.6");
resolutionList.Add("1.7");
resolutionList.Add("1.8");
SfComboBox comboBox = new SfComboBox();
comboBox.HeightRequest = 40;
comboBox.DataSource = resolutionList;
layout.Children.Add(comboBox);
Content = layout;
}
Output

FFind the sample link..