Skip to main content

ItemsEditBase.SelectionChangedCommand Property

Specifies the command executed when an item in the drop-down list is selected.

Namespace: DevExpress.Maui.Editors

Assembly: DevExpress.Maui.Editors.dll

NuGet Package: DevExpress.Maui.Editors

Declaration

public ICommand SelectionChangedCommand { get; set; }

Property Value

Type Description
ICommand

A command that implements the ICommand interface.

Remarks

You can also handle the ItemsEditBase.SelectionChanged event to perform custom actions when a user selects items in the editor.

Example

The following example shows how to use the SelectionChangedCommand to respond user selection actions in an AutoCompleteEdit. In this example, the Page shows an alert with information about the selected item object. To access the AutoCompleteEdit‘s selected item, use the SelectedItem property:

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:dxe="clr-namespace:DevExpress.Maui.Editors;assembly=DevExpress.Maui.Editors"
             xmlns:dxc="clr-namespace:DevExpress.Maui.Controls;assembly=DevExpress.Maui.Controls"
             xmlns:local="clr-namespace:EditorsGetStarted"
             x:Class="EditorsGetStarted.MainPage"
             BackgroundColor="{DynamicResource SecondaryColor}">
    <ScrollView Padding="{OnPlatform iOS='30,60,30,30', Default='30'}">
        <StackLayout Padding="12">
            <dxe:AutoCompleteEdit x:Name="autocomplete" DisplayMember="Name" 
                                  SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
                                  SelectionChangedCommand="{Binding SelectionChangedCommand}">
                <dxe:AutoCompleteEdit.ItemsSourceProvider>
                    <dxe:FilteredItemsSourceProvider ItemsSource="{Binding ItemsSource}"
                                                     FilterCondition="Contains"
                                                     FilterComparisonType="CurrentCultureIgnoreCase"
                                                     FilterMembers="Name, Capital" />
                </dxe:AutoCompleteEdit.ItemsSourceProvider>
                <dxe:AutoCompleteEdit.ItemTemplate>
                    <DataTemplate>
                        <Grid ColumnDefinitions="*,*,*">
                            <Label Padding="10" Text="{Binding Name}" FontAttributes="Bold"/>
                            <Label Padding="10" Grid.Column="1" Text="{Binding Abbr}"/>
                            <Label Padding="10" Grid.Column="2" Text="{Binding Capital}" HorizontalTextAlignment="End"/>
                        </Grid>
                    </DataTemplate>
                </dxe:AutoCompleteEdit.ItemTemplate>
            </dxe:AutoCompleteEdit>
        </StackLayout>
    </ScrollView>
</ContentPage>
using Microsoft.Maui.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;

namespace EditorsGetStarted {
    public partial class MainPage : ContentPage {
        readonly MainViewModel viewModel;
        public MainPage() {
            InitializeComponent();
            BindingContext = new AutoCompleteEditViewModel(this);
        }
    }

    public class AutoCompleteEditViewModel : INotifyPropertyChanged {
        Page page;

        ICommand selectionChangedCommand;
        public ICommand SelectionChangedCommand { get { return selectionChangedCommand; } set { selectionChangedCommand = value; OnPropertyChanged("SelectionChangedCommand"); } }

        State selectedItem;
        public State SelectedItem { get { return selectedItem; } set { selectedItem = value; OnPropertyChanged("SelectedItem"); } }

        List<State> itemsSource = new List<State>();

        public List<State> ItemsSource { get { return itemsSource; } set { itemsSource = value; OnPropertyChanged("ItemsSource"); } }

        public AutoCompleteEditViewModel(Page page) {
            this.page = page;
            ItemsSource.Add(new State() { Name = "California", Abbr = "CA", Capital = "Sacramento" });
            ItemsSource.Add(new State() { Name = "Colorado", Abbr = "CO", Capital = "Denver" });
            ItemsSource.Add(new State() { Name = "Connecticut", Abbr = "CT", Capital = "Hartford" });
            SelectionChangedCommand = new Command(SelectionChanged);
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "") {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        private async void SelectionChanged() {
            // Get the selected item:
            State selectedItem = SelectedItem;

            // Add your custom logic over the AutoCompleteEdit's selected item here.
            // For example, you can show a message about the selected item:
            await page.DisplayAlert("Selected item info", $"{selectedItem.Name} ({selectedItem.Abbr}) is selected.", "OK");
        }
    }
    public class State {
        public string Name { get; init; }
        public string Abbr { get; init; }
        public string Capital { get; init; }
    }
}
See Also