<ContentPage.BindingContext>
<local:EmployeeViewModel/> </ContentPage.BindingContext> <StackLayout VerticalOptions="Start" HorizontalOptions="Start" Padding="30"> <autocomplete:SfAutoComplete HeightRequest="40" x:Name="autoComplete" DisplayMemberPath="Name" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" DataSource="{Binding EmployeeCollection}" /> </StackLayout> |
public class EmployeeViewModel : INotifyPropertyChanged
{ private ObservableCollection<Employee> employeeCollection; public ObservableCollection<Employee> EmployeeCollection { get { return employeeCollection; } set { employeeCollection = value; } } private object selectedItem; public event PropertyChangedEventHandler PropertyChanged; public object SelectedItem { get { return selectedItem; } set { selectedItem = value; NotifyPropertyChanged(); } } private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } //ViewModel property for MultiSelect Mode //private ObservableCollection<object> selectedItems; //public ObservableCollection<object> SelectedItems //{ // get { return selectedItems; } // set { selectedItems = value; NotifyPropertyChanged(); } //} public EmployeeViewModel() { employeeCollection = new ObservableCollection<Employee>(); employeeCollection.Add(new Employee() { ID = 1, Name = "Eric" }); employeeCollection.Add(new Employee() { ID = 2, Name = "James" }); employeeCollection.Add(new Employee() { ID = 3, Name = "Jacob" }); employeeCollection.Add(new Employee() { ID = 4, Name = "Lucas" }); employeeCollection.Add(new Employee() { ID = 5, Name = "Mark" }); employeeCollection.Add(new Employee() { ID = 6, Name = "Aldan" }); employeeCollection.Add(new Employee() { ID = 7, Name = "Aldrin" }); employeeCollection.Add(new Employee() { ID = 8, Name = "Alan" }); employeeCollection.Add(new Employee() { ID = 9, Name = "Aaron" }); //Assigned initial SelectedItem for AutoComplete. SelectedItem = employeeCollection[0]; //Assigned initial SelectedItems for AutoComplete in MultiSelect mode. //SelectedItems = new ObservableCollection<object>(); //SelectedItems.Add(employeeCollection[0]); //SelectedItems.Add(employeeCollection[1]); //SelectedItems.Add(employeeCollection[2]); } } |