We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SfComboBox SelectionChanged MVVM Binding To ICommand

Thread ID:

Created:

Updated:

Platform:

Replies:

150744 Jan 17,2020 07:36 PM UTC Jan 21,2020 10:16 AM UTC Xamarin.Forms 3
loading
Tags: SfComboBox
Matthew Bailey
Asked On January 17, 2020 07:36 PM UTC

I have a need to link SfComboBox's SelectionChanged to an ICommand in my ViewModel.
I already know that there's the Behavior Method as linked here. - but how do I control it from a ViewModel instead so that I have access to all my other bindings rather than using a method in the code-behind?

Hemalatha Marikumar [Syncfusion]
Replied On January 20, 2020 12:35 PM UTC

Hi Matthew,

Greetings from Syncfusion.

We have analyzed your query and you can trigger the ComboBox Selection Changed event in ViewModel as like below code snippet.

ViewModel: 
  public class MainPageViewModel : ViewModelBase, INavigationAware 
    { 
        public DelegateCommand<object> SelectionChangedCommand { getset; } 
  
  
        private ObservableCollection<Employee> employeeCollection; 
        public ObservableCollection<Employee> EmployeeCollection 
        { 
            get { return employeeCollection; } 
            set { employeeCollection = value; } 
        } 
        INavigationService _navigationService; 
        public MainPageViewModel(INavigationService navigationService) : base(navigationService) 
        { 
            SelectionChangedCommand = new DelegateCommand<object>(SetContent); 
            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" }); 
        } 
  
        public void SetContent(object obj) 
        { 
  
        } 
    } 
  
XAML: 
<combobox:SfComboBox IsEditableMode="True" DataSource="{Binding EmployeeCollection}" MaximumDropDownHeight="300"  ShowSuggestionsOnFocus="True" DisplayMemberPath="Name" x:Name="comboBox"> 
            <combobox:SfComboBox.Behaviors> 
                <local:EventToCommandBehavior Command="{Binding SelectionChangedCommand}"  EventName="SelectionChanged"/> 
            </combobox:SfComboBox.Behaviors> 
        </combobox:SfComboBox> 

We have created sample based on your requirement please find the sample form below location.

Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxCommand613176966.zip 
 
 
Regards, 
Hemalatha M. 


Matthew Bailey
Replied On January 20, 2020 12:56 PM UTC

Thank you for the reply,

but is there a way to do this without incorporating Prism's INavigationAware interface?

Hemalatha Marikumar [Syncfusion]
Replied On January 21, 2020 10:16 AM UTC

Hi Mathew,

We have analyzed your query and created sample based on your requirement without using prism. Please download the sample from below location.

Sample link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/ComboBoxCommand-2054739153.zip 
 
Code snippet: 
public class MainPageViewModel 
    { 
        public ICommand SelectionChangedCommand { getset; } 
  
        private ObservableCollection<Employee> employeeCollection; 
        public ObservableCollection<Employee> EmployeeCollection 
        { 
            get { return employeeCollection; } 
            set { employeeCollection = value; } 
        } 
  
        public MainPageViewModel() 
        { 
            SelectionChangedCommand = new Command<object>(ComboBoxSelectionChanged); 
            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" }); 
        } 
  
        public  void ComboBoxSelectionChanged(object obj) 
        { 
            var selectionChangedArgs = obj as SelectionChangedEventArgs; 
            var selectedItem = (selectionChangedArgs.Value as Employee).Name; 
        } 
    } 

Please let us know if you have any other queries.

Regards, 
Hemalatha M. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon