Carousel - 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:
Rebind Method
You can refresh the data by invoking the Rebind
method. Use the component reference to call the Rebind
method after you have made the data changes (for example adding, removing items). This is needed in case you are not using Observable data or resetting the collection reference. Calling Rebind
will force the component process the available data anew to reflect the updates.
@* Add/remove an item and rebind the Carousel to react to that change. *@
<TelerikButton OnClick="@AddItem">Add item</TelerikButton>
<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>
<TelerikCarousel @ref="@CarouselRef"
Data="@CarouselData"
Width="400px" Height="200px">
<Template>
<div class="item">ID @(context.ID) : @(context.Text)</div>
</Template>
</TelerikCarousel>
<style>
.item {
background: #3d57d8;
color: #fff;
font: 36px/200px sans-serif;
text-align: center;
}
</style>
@code {
private TelerikCarousel<CarouselModel> CarouselRef;
private List<CarouselModel> CarouselData = new List<CarouselModel>();
void AddItem()
{
CarouselData.Add(
new CarouselModel()
{
ID = 4,
Text = "Text 4"
});
CarouselRef.Rebind();
}
void RemoveItem()
{
CarouselData.RemoveAt(CarouselData.Count() - 1);
CarouselRef.Rebind();
}
protected override void OnInitialized()
{
CarouselData = new List<CarouselModel>
{
new CarouselModel { ID = 1, Text = "Text 1" },
new CarouselModel { ID = 2, Text = "Text 2" },
new CarouselModel { ID = 3, Text = "Text 3" }
};
}
public class CarouselModel
{
public int ID { get; set; }
public string Text { get; set; }
}
}
Observable Data
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.
@* Add/remove an item to see how the Carousel reacts to that change. *@
@using System.Collections.ObjectModel
<TelerikButton OnClick="@AddItem">Add item</TelerikButton>
<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>
<TelerikCarousel Data="@CarouselData"
Width="400px" Height="200px">
<Template>
<div class="item">ID @(context.ID) : @(context.Text)</div>
</Template>
</TelerikCarousel>
<style>
.item {
background: #3d57d8;
color: #fff;
font: 36px/200px sans-serif;
text-align: center;
}
</style>
@code {
private ObservableCollection<CarouselModel> CarouselData = new ObservableCollection<CarouselModel>();
void AddItem()
{
CarouselData.Add(
new CarouselModel()
{
ID = 4,
Text = "Text 4"
});
}
void RemoveItem()
{
CarouselData.RemoveAt(CarouselData.Count() - 1);
}
protected override void OnInitialized()
{
CarouselData = new ObservableCollection<CarouselModel>
{
new CarouselModel { ID = 1, Text = "Text 1" },
new CarouselModel { ID = 2, Text = "Text 2" },
new CarouselModel { ID = 3, Text = "Text 3" }
};
}
public class CarouselModel
{
public int ID { get; set; }
public string Text { get; set; }
}
}
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 ot change the data to see how the Carousel reacts to that change. *@
<TelerikButton OnClick="@AddItem">Add item</TelerikButton>
<TelerikButton OnClick="@RemoveItem">Remove item</TelerikButton>
<TelerikButton OnClick="@LoadNew">Load New Items</TelerikButton>
<TelerikCarousel Data="@CarouselData"
Width="400px" Height="200px">
<Template>
<div class="item">ID @(context.ID) : @(context.Text)</div>
</Template>
</TelerikCarousel>
<style>
.item {
background: #3d57d8;
color: #fff;
font: 36px/200px sans-serif;
text-align: center;
}
</style>
@code {
private List<CarouselModel> CarouselData = new List<CarouselModel>();
private void AddItem()
{
CarouselData.Add(
new CarouselModel()
{
ID = 4,
Text = "Text 4"
});
CarouselData = new List<CarouselModel>(CarouselData);
}
private void RemoveItem()
{
CarouselData.RemoveAt(CarouselData.Count() - 1);
CarouselData = new List<CarouselModel>(CarouselData);
}
private void LoadNew()
{
CarouselData = new List<CarouselModel>
{
new CarouselModel { ID = 4, Text = "New Item 4" },
new CarouselModel { ID = 5, Text = "New Item 5" },
new CarouselModel { ID = 6, Text = "New Item 6" }
};
}
protected override void OnInitialized()
{
CarouselData = new List<CarouselModel>
{
new CarouselModel { ID = 1, Text = "Text 1" },
new CarouselModel { ID = 2, Text = "Text 2" },
new CarouselModel { ID = 3, Text = "Text 3" }
};
}
public class CarouselModel
{
public int ID { get; set; }
public string Text { get; set; }
}
}