Back to Devexpress

ComboBoxEdit Class

mobilecontrols-devexpress-dot-xamarinforms-dot-editors-43251e6b.md

latest21.3 KB
Original Source

ComboBoxEdit Class

A combo box editor.

Namespace : DevExpress.XamarinForms.Editors

Assembly : DevExpress.XamarinForms.Editors.dll

NuGet Package : DevExpress.XamarinForms.Editors

Declaration

csharp
public class ComboBoxEdit :
    ItemsEditBase,
    IComboBoxEditController,
    IItemsController,
    IEditController,
    IElementController

Remarks

ComboBoxEdit allows selection from a drop-down list of predefined items.

It consists of an edit box and drop-down list that appears when a user taps the edit box or a drop-down icon. The editor allows users to quickly locate items by their names (see Item Filtering).

The combo box can also include the following optional elements:

  1. Label 2. Help Text 3. Icons 4. Error Message

Use the ItemsSource property to define a drop-down item list.

xml
<dxe:ComboBoxEdit>
  <dxe:ComboBoxEdit.ItemsSource>
    <x:Array Type="{x:Type x:String}">
      <x:String>Item 1</x:String>
      <x:String>Item 2</x:String>
      <x:String>Item 3</x:String>
    </x:Array>
  </dxe:ComboBoxEdit.ItemsSource>
</dxe:ComboBoxEdit>
cs
comboBox.ItemsSource = new string[]
{
  "Item 1",
  "Item 2",
  "Item 3"
};

The following properties allow you to manage the selected item.

  • SelectedItem - Gets or sets an object that specifies a data source’s item to which the combo box’ selected item corresponds.

  • SelectedIndex - Specifies an index of an item in the data source to which the combo box’ selected item corresponds.

  • SelectedValue - Gets or sets the selected item’s value. Use the ValueMember property to specify the bound data source’s property that contains values.

Use SelectionChangedCommand and SelectionChangedCommandParameter properties to specify the command executed when the selection of an item changes. You can also handle the SelectionChanged event that fires when the selection changes.

Bind to a List of Primitive Types

You can bind the ItemsSource property to a list of primitive types (string, integer values, etc.):

cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace ComboBoxExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            this.BindingContext = new List<string>() { "Milk", "Tea", "Coffee" };
        }
    }
}
xml
<dxe:ComboBoxEdit ItemsSource="{Binding}"/>

Bind to a Collection of Custom Objects

You can also bind the ItemsSource property to a collection of custom objects. Use the DisplayMember and ValueMember properties to specify the names of data source fields that contain captions for items in the drop-down list and their values. To obtain the selected item’s value, you can use the SelectedValue property.

cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace ComboBoxExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            this.BindingContext = new List<Person>() {
                new Person {Name = "Devin", Age = 50, Location = "Atlanta"},
                new Person {Name = "Brenda", Age = 25, Location = "Memphis"},
                new Person {Name = "Sean", Age = 36, Location = "Houston"}
            };
        }
    }

    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Location { get; set; }
    }
}
xml
<dxe:ComboBoxEdit ItemsSource="{Binding}"
                  DisplayMember="Name"
                  ValueMember="Age"/>

Define Item Template

The combo box allows you to define a drop-down list item template with the ItemTemplate property. Use the DisplayMember property to specify the data source field whose values are displayed in the edit box when a user selects an item.

cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace ComboBoxExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            this.BindingContext = new List<Person>() {
                new Person {Name = "Devin", Age = 50, Location = "Atlanta"},
                new Person {Name = "Brenda", Age = 25, Location = "Memphis"},
                new Person {Name = "Sean", Age = 36, Location = "Houston"}
            };
        }
    }

    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Location { get; set; }
    }
}
xml
<dxe:ComboBoxEdit ItemsSource="{Binding}"
                  DisplayMember="Name">
    <dxe:ComboBoxEdit.ItemTemplate>
      <DataTemplate>
        <Grid>
          <Label Padding="10" Text="{Binding Name}" FontAttributes="Bold" />
          <Label Padding="10" Grid.Column="1" Text="{Binding Age}" />
          <Label Padding="10" Grid.Column="2" Text="{Binding Location}" HorizontalTextAlignment="End" />
        </Grid>
      </DataTemplate>
    </dxe:ComboBoxEdit.ItemTemplate>
</dxe:ComboBoxEdit>

Item Filtering

Enable the IsFilterEnabled property to allow users to search for drop-down items by their display text.

The following list shows related properties and events:

Label

A label is the editor’s input prompt string (LabelText). Editors display this string inside the edit box (when the editor is empty and not focused) or at the top of the editor.

To pin the label to the top edge of the editor box, set the IsLabelFloating property to false.

To customize the label’s appearance settings, use the following properties:

|

Property

|

Description

| | --- | --- | |

LabelColor / FocusedLabelColor
DisabledLabelColor / ErrorColor

|

Specify the label color for each state of the combo-box editor.

| |

LabelFontSize
TextFontFamily
TextFontAttributes

|

Configure the label font settings.

|

Help Text and Error Message

You can display the following labels below an editor:

The BottomTextTopIndent property specifies the indent between the editor’s bottom border and the help or error text.

To specify the color and font attributes for the help/error text, use the following properties:

|

Property

|

Description

| | --- | --- | |

HelpTextColor
DisabledHelpTextColor

|

Specify the help text color for different states of an editor.

| |

ErrorColor

|

Specifies the error message text color.

| |

BottomTextFontSize
BottomTextFontFamily
BottomTextFontAttributes

|

Specify font settings.

|

If HelpText is not set, ErrorText appears as an additional line below the edit box and page content shifts down. To prevent this behavior, set the ReserveBottomTextLine property to true.

Icons

The combo box can display three icons in the edit box.

  • Drop-down/drop-up icon - toggles the visibility of a drop-down list.
  • Error icon - appears in the error state (HasError is true ).
  • Custom icons - can be shown on the left or right within the box.

Use the following members to manage the combo box icons:

|

Icon

|

Property

|

Description

| | --- | --- | --- | |

Drop-Down and Drop-Up Icons

|

DropDownIcon

|

Gets or sets the custom drop-down icon image.

| |

DropUpIcon

|

Gets or sets the custom drop-up icon image.

| |

IsDropDownIconVisible

|

Specifies the drop-down/drop-up icon visibility.

| |

Error Icon

|

ErrorIcon

|

Specifies the error icon image.

| |

ErrorIconClicked / ErrorIconCommand

|

Allow you to perform an action when a user clicks the error icon.

| |

ErrorIconColor

|

Gets or sets the color of an icon displayed when HasError is set to true.

| |

Custom Icons

|

StartIcon / EndIcon

|

Specify custom icons.

| |

StartIconClicked / StartIconCommand
EndIconClicked / EndIconCommand

|

Allow you to perform an action when a user clicks a custom icon.

| |

StartIconColor / EndIconColor

|

Specify icon colors.

| |

IsStartIconVisible / IsEndIconVisible

|

Specify icon visibility.

| |

Common

|

IconColor / DisabledIconColor

|

Specify icon colors for different states of the text editor.

| |

IconIndent

|

Gets or sets the distance between an icon and input text (affix).

| |

IconSpacing

|

Gets or sets the distance between icons.

| |

IconVerticalAlignment

|

Gets or sets the vertical alignment of icons.

|

User Interaction

Editors raise the following events on user interaction:

  • Tap - Fires when the user taps the editor.
  • DoubleTap - Fires when the user double taps the editor.
  • LongPress - Fires when the user presses and holds the editor.

Editor Appearance

You can customize the editor’s appearance settings for different states using the following properties:

|

Combo Box State

|

Properties

| | --- | --- | |

Default

|

BorderColor
BorderThickness
BackgroundColor
TextColor

| |

Focused

|

FocusedBorderColor
FocusedBorderThickness
DropDownBackgroundColor
DropDownItemTextColor
DropDownSelectedItemBackgroundColor
DropDownSelectedItemTextColor

| |

Disabled

|

DisabledBorderColor
DisabledBorderThickness
DisabledBackgroundColor
DisabledTextColor

| |

Error

|

ErrorColor
ErrorIconColor

|

The following list shows common appearance settings applied in all states:

|

Property

|

Description

| | --- | --- | |

CornerMode

|

Gets or sets whether edit box corners are rounded or cut.

| |

CornerRadius

|

Specifies the radius of the combo box and drop-down list corners.

| |

TextFontAttributes
TextFontFamily
TextFontSize

|

Adjust the font settings.

|

Example

The following example shows how to customize combo box appearance and set up item filtering.

|

State

|

Appearance

| | --- | --- | |

Unfocused

|

| |

Focused

|

| |

Filtered

|

|

xml
<dxe:ComboBoxEdit ItemsSource="{Binding}"

                  LabelText="Employee"
                  IsLabelFloating="True"
                  HelpText="Select an e-mail"
                  BorderColor="Black"
                  FocusedBorderColor="DarkOrange"
                  BackgroundColor="Beige"
                  LabelColor="Black"
                  FocusedLabelColor="DarkOrange"
                  CornerRadius="10"
                  CornerMode="Cut"
                  DropDownBackgroundColor="Beige"
                  DropDownSelectedItemBackgroundColor="Brown"
                  DropDownSelectedItemTextColor="White"

                  IsFilterEnabled="True"
                  FilterMode="StartsWith"
                  FilterComparisonType="CurrentCultureIgnoreCase">

  <dxe:ComboBoxEdit.ItemTemplate>
    <DataTemplate>
      <Grid>
        <Label Margin="10,15,0,15" Text="{Binding Name}" FontAttributes="Bold"/>
        <Label Margin="0,15,0,15" Grid.Column="1" Text="{Binding Age}"/>
        <Label Margin="0,15,10,15" Grid.Column="2" Text="{Binding Location}" HorizontalTextAlignment="End"/>
      </Grid>
    </DataTemplate>
  </dxe:ComboBoxEdit.ItemTemplate>
</dxe:ComboBoxEdit>
cs
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace ComboBoxExample {
    public partial class MainPage : ContentPage {
        public MainPage() {
            InitializeComponent();
            this.BindingContext = new List<Person>() {
                new Person {Name = "Jane", Age = 43, Location = "Atlanta"},
                new Person {Name = "Margaret", Age = 25, Location = "Memphis"},
                new Person {Name = "Debbie", Age = 28, Location = "New-York"},
                new Person {Name = "John", Age = 19, Location = "Detroit"},
                new Person {Name = "Derek", Age = 36, Location = "Houston"}
            };
        }
    }

    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Location { get; set; }
    }
}

Implements

Xamarin.Forms.IElementController

Inheritance

Object EditBase ItemsEditBase ComboBoxEdit

See Also

ComboBoxEdit Members

DevExpress.XamarinForms.Editors Namespace