DxTreeList.SelectedDataItem Property
In single selection mode, this property specifies the data item that corresponds to the selected TreeList row.
Namespace: DevExpress.Blazor
Assembly: DevExpress.Blazor.v24.2.dll
NuGet Package: DevExpress.Blazor
Declaration
[DefaultValue(null)]
[Parameter]
public object SelectedDataItem { get; set; }
Property Value
Type | Default | Description |
---|---|---|
Object | null | The data item. |
Remarks
Set the SelectionMode property to Single
to enable single row selection. To access the data item that corresponds to the selected row, use the SelectedDataItem
property as follows:
- Implement two-way binding for this property (
@bind-SelectedDataItem
) to specify the initially selected row and automatically update the property value when selection changes. - Use one-way binding for this property and handle the SelectedDataItemChanged event to implement a custom response to selection changes.
If SelectionMode is set to Multiple
(the default value), use the SelectedDataItems property instead. If you change the selection mode dynamically, specify the SelectedDataItems property and handle the SelectedDataItemsChanged event. In the event handler, check the selection mode and assign the last selected item or all selected items to the SelectedDataItems property.
For more information about selection in the TreeList component, refer to the following topic: Selection and Focus in Blazor TreeList.
Select and Deselect Items
A row can be selected and deselected in the following ways:
- A user clicks a row, taps it, or executes keyboard shortcuts to select or deselect the row. To enable this functionality, set the AllowSelectRowByClick property to
true
. - A user clicks a radio button in the selection column. To display this column, declare a DxTreeListSelectionColumn object in the Columns template.
- Call the
Select*
orDeselect*
method. Refer to the TreeList’s member table for a list of available methods.
Obtain Item Field Values
Pass the SelectedDataItem
property value to the GetDataItemValue method to get the item’s field value when the TreeList is bound to a collection of anonymous objects. In other cases, you can cast the SelectedDataItem
property value to the corresponding type and use the {DataItem.FieldName}
notation to get the item’s field value.
The following example allows users to click a row to select it and displays information about that row:
@inject EmployeeTaskService EmployeeTaskService
<DxTreeList Data="TreeListData"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
AllowSelectRowByClick="true"
SelectionMode="TreeListSelectionMode.Single"
@bind-SelectedDataItem="@SelectedDataItem">
<Columns>
<DxTreeListDataColumn FieldName="Name" Caption="Task" />
<DxTreeListDataColumn FieldName="EmployeeName" />
<DxTreeListDataColumn FieldName="StartDate" />
<DxTreeListDataColumn FieldName="DueDate" />
</Columns>
</DxTreeList>
<div>
<p><b>Selected task:</b> @((SelectedDataItem as EmployeeTask)?.Name ?? "(none)")</p>
</div>
@code {
List<EmployeeTask> TreeListData { get; set; }
object SelectedDataItem { get; set; }
protected override void OnInitialized() {
TreeListData = EmployeeTaskService.GenerateData();
}
}