Drawer - Refresh Data
The most common reason you would use an ObservableCollection is to make a component (like a grid, treeview, treelist, dropdown) change or react when you change that collection.
When you want to refresh the component data source like that, there are two important framework behaviors you need to be aware of - when ObservableCollection instances fire events, and how to refresh the data of a component when it is not an observable collection.
In this article:
Observable Data
The Drawer does not support binding to observable data. You can currently refresh the component by creating a new collection reference.
Databound components can benefit from live updates - when the data source collection changes, the components should update to reflect that change. Most data-bound components in the Telerik UI for Blazor suite implement such functionality.
When the Data
of the component is a collection that implements the INotifyCollectionChanged
interface (such as ObservableCollection
), the Telerik components subscribe to its CollectionChanged
event to make live update. This means that adding items, removing items, or clearing the collection updates the components (its .Add()
, .Remove()
and .Clear()
methods).
The Observable collections fire the CollectionChanged
event only when their Add
, Remove
and Clear
methods are called. They do not fire it when you change the value of a field of one of their elements.
New Collection Reference
In Blazor, the framework will fire the OnParametersSet
event of a child component (which is how child components can react to outside changes) only when it can detect a change in the object it receives through the corresponding parameter (like Data
for the data sources of Telerik components). This detection works as follows:
For strings and value types, this happens when their value changes.
-
For reference types (such as data collections like
List
, or anyIEnumerable
, and application-specific objects), this happens when the object reference changes.Thus, you would usually need to create a
new
reference for the view-model field (such asTreeViewData = new List<MyTreeViewItem>(theUpdatedDataCollection);
) when you want the component to update.
@* Add/remove an item or change the data collection to see how the Drawer reacts to that change. *@
<TelerikButton OnClick="@AddItem">Add item</TelerikButton>
<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>
<TelerikButton OnClick="@ChangeData">Change data</TelerikButton>
<TelerikDrawer Data="@Data"
MiniMode="true"
Mode="DrawerMode.Push"
@ref="@DrawerRef"
@bind-SelectedItem="@SelectedItem">
<DrawerContent>
<TelerikButton OnClick="@(() => DrawerRef.ToggleAsync())" Icon="@SvgIcon.Menu">Toggle drawer</TelerikButton>
<div class="m-5">
Selected Item: @SelectedItem?.Text
</div>
</DrawerContent>
</TelerikDrawer>
@code {
TelerikDrawer<DrawerItem> DrawerRef { get; set; }
DrawerItem SelectedItem { get; set; }
void AddItem()
{
Data.Add(new DrawerItem { Text = "Info", Icon = SvgIcon.InfoCircle });
Data = new List<DrawerItem>(Data);
}
void RemoveItem()
{
if (Data.Count > 0)
{
Data.RemoveAt(Data.IndexOf(Data.Last()));
Data = new List<DrawerItem>(Data);
}
}
void ChangeData()
{
Data = new List<DrawerItem>
{
new DrawerItem { Text = "Overview", Icon = SvgIcon.InfoCircle },
new DrawerItem { Text = "Events", Icon = SvgIcon.Star },
};
}
List<DrawerItem> Data { get; set; } =
new List<DrawerItem>
{
new DrawerItem { Text = "Counter", Icon = SvgIcon.Plus },
new DrawerItem { Text = "FetchData", Icon = SvgIcon.GridLayout },
};
public class DrawerItem
{
public string Text { get; set; }
public ISvgIcon Icon { get; set; }
}
}