How can I set the color of the checkbox and the label if an Item is checked
There is no specific class for the CheckBox and the only option is to use CSS to style it:
Alright I could figure out how I can set the checkbox colors, but ther is no working idea how to set the colors of the the label if i select a node. Could you please provide me a working example
@using Microsoft.EntityFrameworkCore
@using RadzenBlazorDemos.Models.Northwind
@inherits DbContextPage
<style type="text/css">
/* One place to set the color */
:root { --tree-checked-color: #4CAF50; }
/* Checkbox color when checked */
.rz-treenode-content .rz-chkbox-box.rz-state-active {
background-color: var(--tree-checked-color) !important;
border-color: var(--tree-checked-color) !important;
}
.rz-treenode-content .rz-chkbox-box.rz-state-active .rzi-check {
color: white !important;
}
/* Label color when its checkbox is checked (note the :has) */
.rz-treenode-content:has(.rz-chkbox-box.rz-state-active) .rz-treenode-label {
color: var(--tree-checked-color) !important;
font-weight: 600;
}
</style>
<RadzenRow class="rz-p-0 rz-p-lg-12">
<RadzenColumn Size="12" SizeLG="6" OffsetLG="3">
<RadzenCard>
<RadzenTree AllowCheckBoxes="true" @bind-CheckedValues=@CheckedValues Style="width: 100%; height: 300px" Data=@categories
ItemRender="@TreeItemRender">
<RadzenTreeLevel TextProperty="@nameof(Category.CategoryName)" ChildrenProperty="Products" />
<RadzenTreeLevel TextProperty="@nameof(Product.ProductName)" HasChildren=@(product => false) />
</RadzenTree>
</RadzenCard>
</RadzenColumn>
</RadzenRow>
<EventConsole @ref=@console />
@code {
IEnumerable<Category> categories;
IEnumerable<object> checkedValues;
IEnumerable<object> CheckedValues
{
get => checkedValues;
set
{
checkedValues = value;
if (checkedValues != null)
{
console.Log($"CheckedValues Changed {string.Join(Environment.NewLine, value.Select(GetText))}");
}
}
}
string GetText(object data)
{
if (data is Category category)
{
return category.CategoryName;
}
if (data is Product product)
{
return product.ProductName;
}
return string.Empty;
}
EventConsole console;
Category firstCategory;
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
categories = await Task.FromResult(dbContext.Categories.Include(c => c.Products));
firstCategory = await Task.FromResult(categories.FirstOrDefault());
checkedValues = await Task.FromResult(firstCategory.Products.Where(p => p.ProductName.StartsWith("C")));
}
void TreeItemRender(TreeItemRenderEventArgs args)
{
if (args.Value == firstCategory && !args.Data.Cast<object>().Any())
{
args.Checked = null;
}
}
}
