Table of Contents

Class SelectingItemsControl

Namespace
Avalonia.Controls.Primitives
Assembly
Avalonia.Controls.dll

An ItemsControl that maintains a selection.

public class SelectingItemsControl : ItemsControl, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IInputElement, IDataTemplateHost, ISetterValue, IChildIndexProvider
Inheritance
SelectingItemsControl
Implements
Derived
Inherited Members
Extension Methods

Remarks

SelectingItemsControl provides a base class for ItemsControls that maintain a selection (single or multiple). By default only its SelectedIndex and SelectedItem properties are visible; the current multiple Selection and SelectedItems together with the SelectionMode properties are protected, however a derived class can expose these if it wishes to support multiple selection.

SelectingItemsControl maintains a selection respecting the current SelectionMode but it does not react to user input; this must be handled in a derived class. It does, however, respond to IsSelectedChangedEvent events from items and updates the selection accordingly.

Constructors

SelectingItemsControl()

public SelectingItemsControl()

Fields

AutoScrollToSelectedItemProperty

Defines the AutoScrollToSelectedItem property.

public static readonly StyledProperty<bool> AutoScrollToSelectedItemProperty

Field Value

StyledProperty<bool>

IsSelectedChangedEvent

Event that should be raised by containers when their selection state changes to notify the parent SelectingItemsControl that their selection state has changed.

public static readonly RoutedEvent<RoutedEventArgs> IsSelectedChangedEvent

Field Value

RoutedEvent<RoutedEventArgs>

IsSelectedProperty

Defines the IsSelected attached property.

public static readonly StyledProperty<bool> IsSelectedProperty

Field Value

StyledProperty<bool>

IsTextSearchEnabledProperty

Defines the IsTextSearchEnabled property.

public static readonly StyledProperty<bool> IsTextSearchEnabledProperty

Field Value

StyledProperty<bool>

SelectedIndexProperty

Defines the SelectedIndex property.

public static readonly DirectProperty<SelectingItemsControl, int> SelectedIndexProperty

Field Value

DirectProperty<SelectingItemsControl, int>

SelectedItemProperty

Defines the SelectedItem property.

public static readonly DirectProperty<SelectingItemsControl, object?> SelectedItemProperty

Field Value

DirectProperty<SelectingItemsControl, object>

SelectedItemsProperty

Defines the SelectedItems property.

protected static readonly DirectProperty<SelectingItemsControl, IList?> SelectedItemsProperty

Field Value

DirectProperty<SelectingItemsControl, IList>

SelectedValueBindingProperty

Defines the SelectedValueBinding property

public static readonly StyledProperty<IBinding?> SelectedValueBindingProperty

Field Value

StyledProperty<IBinding>

SelectedValueProperty

Defines the SelectedValue property

public static readonly StyledProperty<object?> SelectedValueProperty

Field Value

StyledProperty<object>

SelectionChangedEvent

Defines the SelectionChanged event.

public static readonly RoutedEvent<SelectionChangedEventArgs> SelectionChangedEvent

Field Value

RoutedEvent<SelectionChangedEventArgs>

SelectionModeProperty

Defines the SelectionMode property.

protected static readonly StyledProperty<SelectionMode> SelectionModeProperty

Field Value

StyledProperty<SelectionMode>

SelectionProperty

Defines the Selection property.

protected static readonly DirectProperty<SelectingItemsControl, ISelectionModel> SelectionProperty

Field Value

DirectProperty<SelectingItemsControl, ISelectionModel>

WrapSelectionProperty

Defines the WrapSelection property.

public static readonly StyledProperty<bool> WrapSelectionProperty

Field Value

StyledProperty<bool>

Properties

AlwaysSelected

Gets a value indicating whether AlwaysSelected is set.

protected bool AlwaysSelected { get; }

Property Value

bool

AutoScrollToSelectedItem

Gets or sets a value indicating whether to automatically scroll to newly selected items.

public bool AutoScrollToSelectedItem { get; set; }

Property Value

bool

IsTextSearchEnabled

Gets or sets a value that specifies whether a user can jump to a value by typing.

public bool IsTextSearchEnabled { get; set; }

Property Value

bool

SelectedIndex

Gets or sets the index of the selected item.

public int SelectedIndex { get; set; }

Property Value

int

SelectedItem

Gets or sets the selected item.

public object? SelectedItem { get; set; }

Property Value

object

SelectedItems

Gets or sets the selected items.

protected IList? SelectedItems { get; set; }

Property Value

IList

Remarks

By default returns a collection that can be modified in order to manipulate the control selection, however this property will return null if Selection is re-assigned; you should only use either Selection or SelectedItems.

SelectedValue

Gets or sets the value of the selected item, obtained using SelectedValueBinding

public object? SelectedValue { get; set; }

Property Value

object

SelectedValueBinding

Gets the IBinding instance used to obtain the SelectedValue property

[AssignBinding]
[InheritDataTypeFromItems("ItemsSource")]
public IBinding? SelectedValueBinding { get; set; }

Property Value

IBinding

Selection

Gets or sets the model that holds the current selection.

protected ISelectionModel Selection { get; set; }

Property Value

ISelectionModel

SelectionMode

Gets or sets the selection mode.

protected SelectionMode SelectionMode { get; set; }

Property Value

SelectionMode

Remarks

Note that the selection mode only applies to selections made via user interaction. Multiple selections can be made programmatically regardless of the value of this property.

WrapSelection

Gets or sets a value which indicates whether to wrap around when the first or last item is reached.

public bool WrapSelection { get; set; }

Property Value

bool

Methods

BeginInit()

public override void BeginInit()

ClearContainerForItemOverride(Control)

protected override void ClearContainerForItemOverride(Control element)

Parameters

element Control

ContainerForItemPreparedOverride(Control, object?, int)

Called when a container has been fully prepared to display an item.

protected override void ContainerForItemPreparedOverride(Control container, object? item, int index)

Parameters

container Control

The container control.

item object

The item being displayed.

index int

The index of the item being displayed.

Remarks

This method will be called when a container has been fully prepared and added to the logical and visual trees, but may be called before a layout pass has completed. It is called immediately before the ContainerPrepared event is raised.

ContainerIndexChangedOverride(Control, int, int)

Called when the index for a container changes due to an insertion or removal in the items collection.

protected override void ContainerIndexChangedOverride(Control container, int oldIndex, int newIndex)

Parameters

container Control

The container whose index changed.

oldIndex int

The old index.

newIndex int

The new index.

EndInit()

public override void EndInit()

GetContainerFromEventSource(object?)

Tries to get the container that was the source of an event.

protected Control? GetContainerFromEventSource(object? eventSource)

Parameters

eventSource object

The control that raised the event.

Returns

Control

The container or null if the event did not originate in a container.

GetIsSelected(Control)

Gets the value of the IsSelectedProperty on the specified control.

public static bool GetIsSelected(Control control)

Parameters

control Control

The control.

Returns

bool

The value of the attached property.

MoveSelection(Control?, NavigationDirection, bool, bool)

Moves the selection in the specified direction relative to the specified container.

protected bool MoveSelection(Control? from, NavigationDirection direction, bool wrap = false, bool rangeModifier = false)

Parameters

from Control

The container which serves as a starting point for the movement.

direction NavigationDirection

The direction to move.

wrap bool

Whether to wrap when the selection reaches the first or last item.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

Returns

bool

True if the selection was moved; otherwise false.

MoveSelection(NavigationDirection, bool, bool)

Moves the selection in the specified direction relative to the current selection.

protected bool MoveSelection(NavigationDirection direction, bool wrap = false, bool rangeModifier = false)

Parameters

direction NavigationDirection

The direction to move.

wrap bool

Whether to wrap when the selection reaches the first or last item.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

Returns

bool

True if the selection was moved; otherwise false.

OnApplyTemplate(TemplateAppliedEventArgs)

Called when the control's template is applied. In simple terms, this means the method is called just before the control is displayed.

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

Parameters

e TemplateAppliedEventArgs

The event args.

OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is added to a rooted visual tree.

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnDataContextBeginUpdate()

Called when the DataContext begins updating.

protected override void OnDataContextBeginUpdate()

OnDataContextEndUpdate()

Called when the DataContext finishes updating.

protected override void OnDataContextEndUpdate()

OnInitialized()

Called when the control finishes initialization.

protected override void OnInitialized()

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

OnTextInput(TextInputEventArgs)

Called before the TextInput event occurs.

protected override void OnTextInput(TextInputEventArgs e)

Parameters

e TextInputEventArgs

The event args.

PrepareContainerForItemOverride(Control, object?, int)

Prepares the specified element to display the specified item.

protected override void PrepareContainerForItemOverride(Control container, object? item, int index)

Parameters

container Control

The element that's used to display the specified item.

item object

The item to display.

index int

The index of the item to display.

SetIsSelected(Control, bool)

Gets the value of the IsSelectedProperty on the specified control.

public static void SetIsSelected(Control control, bool value)

Parameters

control Control

The control.

value bool

The value of the property.

UpdateDataValidation(AvaloniaProperty, BindingValueType, Exception?)

Called to update the validation state for properties for which data validation is enabled.

protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)

Parameters

property AvaloniaProperty

The property.

state BindingValueType

The current data binding state.

error Exception

The current data binding error, if any.

UpdateSelection(Control, bool, bool, bool, bool, bool)

Updates the selection for a container based on user interaction.

protected void UpdateSelection(Control container, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false, bool fromFocus = false)

Parameters

container Control

The container.

select bool

Whether the container should be selected or unselected.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

toggleModifier bool

Whether the toggle modifier is enabled (i.e. ctrl key).

rightButton bool

Whether the event is a right-click.

fromFocus bool

Wheter the event is a focus event

UpdateSelection(int, bool, bool, bool, bool, bool)

Updates the selection for an item based on user interaction.

protected void UpdateSelection(int index, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false, bool fromFocus = false)

Parameters

index int

The index of the item.

select bool

Whether the item should be selected or unselected.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

toggleModifier bool

Whether the toggle modifier is enabled (i.e. ctrl key).

rightButton bool

Whether the event is a right-click.

fromFocus bool

Wheter the event is a focus event

UpdateSelectionFromEventSource(object?, bool, bool, bool, bool, bool)

Updates the selection based on an event that may have originated in a container that belongs to the control.

protected bool UpdateSelectionFromEventSource(object? eventSource, bool select = true, bool rangeModifier = false, bool toggleModifier = false, bool rightButton = false, bool fromFocus = false)

Parameters

eventSource object

The control that raised the event.

select bool

Whether the container should be selected or unselected.

rangeModifier bool

Whether the range modifier is enabled (i.e. shift key).

toggleModifier bool

Whether the toggle modifier is enabled (i.e. ctrl key).

rightButton bool

Whether the event is a right-click.

fromFocus bool

Wheter the event is a focus event

Returns

bool

True if the event originated from a container that belongs to the control; otherwise false.

Events

SelectionChanged

Occurs when the control's selection changes.

public event EventHandler<SelectionChangedEventArgs>? SelectionChanged

Event Type

EventHandler<SelectionChangedEventArgs>