Class SelectableCollectionViewModel<T>
View model class for representing a collection of objects that implements ISelectable. By default it only allows one item to be selected at a time and it will manage the IsSelected property of the items in the collection. If you want to allow multiple selections, set AllowMultipleSelections to true.
Inheritance
Implements
Inherited Members
Namespace: Benday.Presentation
Assembly: Benday.Presentation.dll
Syntax
public class SelectableCollectionViewModel<T> : ViewModelBase, INotifyPropertyChanged where T : class, ISelectable
Type Parameters
| Name | Description |
|---|---|
| T |
Constructors
| Edit this page View SourceSelectableCollectionViewModel()
Declaration
public SelectableCollectionViewModel()
SelectableCollectionViewModel(ObservableCollection<T>)
Initializes a new instance of the SelectableCollectionViewModel class with existing items or an existing ObservableCollection instance.
Declaration
public SelectableCollectionViewModel(ObservableCollection<T> values)
Parameters
| Type | Name | Description |
|---|---|---|
| ObservableCollection<T> | values |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
SelectableCollectionViewModel(ObservableCollection<T>, T)
Initializes a new instance of the SelectableCollectionViewModel class with existing items or an existing ObservableCollection instance.
Declaration
public SelectableCollectionViewModel(ObservableCollection<T> values, T selectedItem)
Parameters
| Type | Name | Description |
|---|---|---|
| ObservableCollection<T> | values | |
| T | selectedItem | Item in the values collection that should be selected |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
Fields
| Edit this page View SourceItemsPropertyName
Declaration
public readonly string ItemsPropertyName
Field Value
| Type | Description |
|---|---|
| string |
_Items
Declaration
protected ObservableCollection<T> _Items
Field Value
| Type | Description |
|---|---|
| ObservableCollection<T> |
Properties
| Edit this page View SourceAllowMultipleSelections
Gets or sets a value indicating whether multiple selections are allowed.
Declaration
public bool AllowMultipleSelections { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
BeforeSetSelectedItem
Gets or sets a delegate that is called before the selected item is changed. Return false from the delegate to cancel the selection change.
Declaration
public SelectableCollectionViewModel<T>.BeforeSetSelectedItemDelegate? BeforeSetSelectedItem { get; set; }
Property Value
| Type | Description |
|---|---|
| SelectableCollectionViewModel<T>.BeforeSetSelectedItemDelegate |
Count
Gets the number of items in the collection.
Declaration
public int Count { get; }
Property Value
| Type | Description |
|---|---|
| int |
HasOnItemSelectedSubscriber
Utility property to determine if the view model has a subscriber to the OnItemSelected event.
Declaration
public bool HasOnItemSelectedSubscriber { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IgnoreRequestsToSetNullValuesAsSelected
Gets or sets a value indicating whether requests to set null values as the selected item should be ignored. When true, setting SelectedItem to null will have no effect. Default is false.
Declaration
public bool IgnoreRequestsToSetNullValuesAsSelected { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
IsEnabled
Gets or sets a value indicating whether this view model is enabled.
Declaration
public bool IsEnabled { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
IsValid
Gets or sets a value indicating whether this view model is valid. NOTE: this property does not do any validation, it is just a flag that can be used by the view.
Declaration
public bool IsValid { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
Items
Gets or sets the collection of items.
Declaration
public ObservableCollection<T> Items { get; protected set; }
Property Value
| Type | Description |
|---|---|
| ObservableCollection<T> |
SelectedItem
Gets or sets the selected item.
Declaration
public T? SelectedItem { get; set; }
Property Value
| Type | Description |
|---|---|
| T |
Methods
| Edit this page View SourceAdd(T)
Adds an item to the collection.
Declaration
public void Add(T addThis)
Parameters
| Type | Name | Description |
|---|---|---|
| T | addThis |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
Clear()
Clears the collection.
Declaration
public void Clear()
GetSelectedItems()
Gets the selected items.
Declaration
public List<T> GetSelectedItems()
Returns
| Type | Description |
|---|---|
| List<T> | Selected items in a new instance of List |
IndexOf(T)
Returns the index of the specified item in the collection.
Declaration
public int IndexOf(T item)
Parameters
| Type | Name | Description |
|---|---|---|
| T | item | The item to locate. |
Returns
| Type | Description |
|---|---|
| int | The zero-based index of the item, or -1 if not found. |
Initialize(IEnumerable<T>)
Resets the collection to the specified values.
Declaration
public virtual void Initialize(IEnumerable<T> values)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | values |
Move(int, int)
Moves an item from one position to another in the collection.
Declaration
public void Move(int draggedIndex, int targetIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | draggedIndex | The current index of the item to move. |
| int | targetIndex | The index to move the item to. |
OnAfterMove(int, int)
Raises the AfterMove event.
Declaration
protected virtual void OnAfterMove(int draggedIndex, int targetIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | draggedIndex | The original index of the moved item. |
| int | targetIndex | The new index of the moved item. |
OnItemPropertyChanged(object?, PropertyChangedEventArgs)
Declaration
protected virtual void OnItemPropertyChanged(object? sender, PropertyChangedEventArgs e)
Parameters
| Type | Name | Description |
|---|---|---|
| object | sender | |
| PropertyChangedEventArgs | e |
Remove(T)
Removes an item from the collection.
Declaration
public void Remove(T removeThis)
Parameters
| Type | Name | Description |
|---|---|---|
| T | removeThis |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException |
Events
| Edit this page View SourceAfterMove
Event raised after an item has been moved within the collection.
Declaration
public event EventHandler<MoveEventArgs<T>>? AfterMove
Event Type
| Type | Description |
|---|---|
| EventHandler<MoveEventArgs<T>> |
OnItemSelected
Event that is raised when an item is selected.
Declaration
public event ItemSelectedEventHandler<T>? OnItemSelected
Event Type
| Type | Description |
|---|---|
| ItemSelectedEventHandler<T> |