How to use behaviours for changed event in Xamarin.Forms ComboBox?
You can use Behaviors for the SelectionChanged event of Xamarin.Forms ComboBox control by following the given steps:
Step 1: Add the necessary assemblies in the PCL, Android, iOS, and UWP projects. Refer to this UG documentation to know more about the assemblies required for adding SfComboBox control to your project.
Step 2: Add the ComboBox control to the content view of the main page using the following code.
XAML
<StackLayout Padding="50,100,50,0"> <comboBox:SfComboBox x:Name="comboBox" MultiSelectMode="Token" DisplayMemberPath="Name" DataSource="{Binding EmployeeCollection}" SelectedItem="{Binding MultiSelectedItem,Mode=TwoWay}" MaximumDropDownHeight = "180"> </comboBox:SfComboBox> </StackLayout>
Step 3: Add the necessary Model and ViewModel content and add the following binding context on the MainPage.Cs page.
C#
namespace ComboBox { public partial class MainPage : ContentPage { EmployeeViewModel employeeViewModel; public MainPage() { InitializeComponent(); employeeViewModel = new EmployeeViewModel(); this.BindingContext = employeeViewModel; } } }
Step 4: Add the Behavior code in view model that needs to be used for combo box SelectionChangedEvent.
C#
public class SelectionChangedBehavior : Behavior<SfComboBox> { public Command Display { get; private set; } EmployeeViewModel employeeViewModel; protected override void OnAttachedTo(SfComboBox bindable) { bindable.SelectionChanged += Bindable_SelectionChanged; base.OnAttachedTo(bindable); } protected override void OnDetachingFrom(SfComboBox bindable) { bindable.SelectionChanged -= Bindable_SelectionChanged; base.OnDetachingFrom(bindable); } void Bindable_SelectionChanged(object sender, Syncfusion.XForms.ComboBox.SelectionChangedEventArgs e) { employeeViewModel = new EmployeeViewModel(); var selectedValue = e.Value as List<object>; var lastItem = selectedValue.Last(); employeeViewModel.SelectedId = new List<string>(); foreach (var item in selectedValue) { if (item == lastItem) { var selId = item as Employee; employeeViewModel.SelectedId.Add(selId.Id); Application.Current.MainPage.DisplayAlert("Selected Id", "The last selected ID is " + selId.Id, "Ok"); } else { var selId = item as Employee; employeeViewModel.SelectedId.Add(selId.Id); } } } }
Add the same behavior to your SfComboBox Xaml code.
XAML
<StackLayout Padding="50,100,50,0"> <comboBox:SfComboBox x:Name="comboBox" MultiSelectMode="Token" DisplayMemberPath="Name" DataSource="{Binding EmployeeCollection}" SelectedItem="{Binding MultiSelectedItem,Mode=TwoWay}" MaximumDropDownHeight = "180"> <comboBox:SfComboBox.Behaviors> <local:SelectionChangedBehavior/> </comboBox:SfComboBox.Behaviors> </comboBox:SfComboBox> </StackLayout>
The following screenshot illustrates the output.
|
Please download the sample for using Behaviors for the SelectionChanged event of SfComboBox: Sample.
Conclusion
I hope you enjoyed learning about how to use behaviours for changed event in Xamarin.Forms ComboBox.
You can refer to our Xamarin.Forms ComboBox feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ComboBox documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!