blazor-405044-components-treelist-appearance-customization-templates.md
TreeList templates are RenderFragment<TValue> type properties. These render fragments can contain both markup and other Razor components. A template’s Razor markup can access the implicit context parameter that is derived from the TValue type and contains template-related members.
<DxTreeList Data="TreeListData" KeyFieldName="Id" ParentKeyFieldName="ParentId">
<Columns> @* ... *@ </Columns>
<ToolbarTemplate>
<DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Contained">
<Items>
<DxToolbarItem Text="Auto Fit Columns" Click="() => context.TreeList.AutoFitColumnWidths()" />
</Items>
</DxToolbar>
</ToolbarTemplate>
</DxTreeList>
You can use the Context attribute to change the parameter name. This name change is important when you nest components that contain RenderFragment<TValue> properties. Otherwise, the following error can occur: The child content element ‘ChildContent’ of component ‘X’ uses the same parameter name (‘context’) ….
<DxTreeList Data="TreeListData" ChildrenFieldName="Satellites" EditMode="TreeListEditMode.EditForm">
<Columns> @* ... *@ </Columns>
<EditFormTemplate Context="editFormContext">
<DxFormLayout>
<DxFormLayoutItem Caption="Name:">
@editFormContext.GetEditor("Name")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Type:">
@editFormContext.GetEditor("TypeOfObject")
</DxFormLayoutItem>
</DxFormLayout>
</EditFormTemplate>
</DxTreeList>
Run Demo: OverviewRun Demo: Auto Fit
This section lists data column properties that allow you to create templates for various elements.
| Element | Property |
|---|---|
| Column Header Caption | HeaderCaptionTemplate |
| Filter Row Cell | FilterRowCellTemplate |
| Cell (Edit Mode) | CellEditTemplate |
| Cell (Display Mode) | CellDisplayTemplate |
| Column Footer | FooterTemplate |
| Column Filter Menu | FilterMenuTemplate |
This section lists command column properties that allow you to create templates for various elements.
| Element | Property |
|---|---|
| Column Header | HeaderCaptionTemplate, HeaderTemplate |
| Filter Row Cell | FilterRowCellTemplate |
| Cell (Edit Mode) | CellEditTemplate |
| Cell (Display Mode) | CellDisplayTemplate |
| Column Footer | FooterTemplate |
This section lists selection column properties that allow you to create templates for various elements.
| Element | Property |
|---|---|
| Column Header | HeaderCaptionTemplate, HeaderTemplate |
| Filter Row Cell | FilterRowCellTemplate |
| Cell (Display Mode) | CellDisplayTemplate |
| Column Footer | FooterTemplate |
This section lists template properties available on the component level. These properties serve two main purposes:
| Element | TreeList Property |
|---|---|
| Search Box | SearchBoxTemplate |
| Edit Form | EditFormTemplate |
| Cell (Display Mode) | DataColumnCellDisplayTemplate |
| Cell (Edit Mode) | DataColumnCellEditTemplate |
| Empty Data Area | EmptyDataAreaTemplate |
| Column Header Caption | ColumnHeaderCaptionTemplate |
| Toolbar | ToolbarTemplate |
| Filter Row Cell | DataColumnFilterRowCellTemplate |
| Filter Builder | FilterBuilderTemplate |
| Column Filter Menu | DataColumnFilterMenuTemplate |
| Column Footer | ColumnFooterTemplate |
This section contains code samples that demonstrate template functionality.
You can specify a template as a method that returns required content (RenderFragment<TValue>). If the method returns null, the TreeList renders the default element.
The following code snippet demonstrates two options used to specify the template - in the markup and in the AfterRender method.
@inject EmployeeTaskService EmployeeTaskService
<DxTreeList @ref="TreeList"
Data="TreeListData"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
ShowFilterRow="true">
<Columns>
<DxTreeListDataColumn FieldName="Name" Caption="Task" />
<DxTreeListDataColumn FieldName="EmployeeName" FilterRowCellTemplate="GetFilterTemplate(false)" />
<DxTreeListDataColumn FieldName="StartDate" FilterRowCellTemplate="GetFilterTemplate(true)" />
<DxTreeListDataColumn FieldName="DueDate" FilterRowCellTemplate="GetFilterTemplate(true)" />
</Columns>
</DxTreeList>
@code {
ITreeList TreeList { get; set; }
List<EmployeeTask> TreeListData { get; set; }
protected override void OnInitialized() {
TreeListData = EmployeeTaskService.GenerateData();
}
protected override void OnAfterRender(bool firstRender) {
if (firstRender) {
TreeList.BeginUpdate();
var column = TreeList.GetDataColumns().First(c => c.FieldName == "Name");
column.CellDisplayTemplate = context => {
return @<text><b>@context.DisplayText</b></text>;
};
column.FilterRowCellTemplate = GetFilterTemplate(false);
TreeList.EndUpdate();
}
base.OnAfterRender(firstRender);
}
RenderFragment<TreeListDataColumnFilterRowCellTemplateContext> GetFilterTemplate(bool isCustom) {
if (!isCustom) return null;
return context => {
return __builder => {
<DxDateEdit Date="(DateTime?)context.FilterRowValue"
NullText="Select a date"
DateChanged="(DateTime? v) => context.FilterRowValue = v" />
};
};
}
}
public class EmployeeTask{
public int Id { get; set; }
public int ParentId { get; set; }
public string Name { get; set; }
public string EmployeeName { get; set; }
public DateTime StartDate { get; set; }
public DateTime DueDate { get; set; }
public EmployeeTask(
int id,
int parentId,
string name,
string employeeName,
DateTime startDate,
DateTime dueDate
)
{
Id = id;
ParentId = parentId;
Name = name;
EmployeeName = employeeName;
StartDate = startDate;
DueDate = dueDate;
}
}
public class EmployeeTaskService {
public List<EmployeeTask> GenerateData() {
return new List<EmployeeTask>() {
new EmployeeTask(1, 0, "Simplify & Clarify Product Messaging", "John Heart", new DateTime(2018, 4, 3), new DateTime(2018, 4, 14)),
new EmployeeTask(2, 1, "Prepare Financial Reports", "Samantha Bright", new DateTime(2018, 4, 3), new DateTime(2018, 4, 7)),
new EmployeeTask(3, 1, "Prepare Marketing Plan", "Arthur Miller", new DateTime(2018, 4, 7), new DateTime(2018, 4, 14)),
new EmployeeTask(4, 0, "Create Action Plan To Improve Customer Engagement", "Robert Reagan", new DateTime(2017, 8, 8), new DateTime(2018, 4, 8)),
new EmployeeTask(5, 4, "Update Personnel Files", "Greta Sims", new DateTime(2017, 8, 8), new DateTime(2017, 10, 18)),
new EmployeeTask(6, 4, "Review Health Insurance Options ", "Brett Wade", new DateTime(2017, 9, 27), new DateTime(2017, 11, 10)),
new EmployeeTask(7, 4, "Choose Between PPO And HMO Health Plan", "Sandra Johnson", new DateTime(2017, 12, 13), new DateTime(2018, 3, 23)),
new EmployeeTask(8, 4, "Update Google Adwords Strategy", "Ed Holmes", new DateTime(2017, 8, 23), new DateTime(2017, 12, 23)),
new EmployeeTask(9, 4, "Create New Brochure Design", "Barb Banks", new DateTime(2018, 1, 3), new DateTime(2018, 3, 14)),
new EmployeeTask(10, 4, "Obtain Price Quote For New Brochure", "Kevin Carter", new DateTime(2018, 2, 1), new DateTime(2018, 3, 15)),
new EmployeeTask(11, 4, "Brochure Design Review", "Cindy Stanwick", new DateTime(2017, 8, 22), new DateTime(2017, 10, 28)),
new EmployeeTask(12, 4, "Review Website Re-Design Strategy", "Sammy Hill", new DateTime(2017, 9, 16), new DateTime(2018, 3, 6)),
new EmployeeTask(13, 4, "Rollout New Website", "Davey Jones", new DateTime(2017, 11, 7), new DateTime(2018, 2, 6)),
new EmployeeTask(14, 4, "Update Sales/Marketing Strategy", "Victor Norris", new DateTime(2017, 12, 13), new DateTime(2018, 4, 2)),
new EmployeeTask(15, 4, "Update Sales/Revenue Report", "Mary Stern", new DateTime(2017, 12, 25), new DateTime(2018, 4, 2)),
new EmployeeTask(16, 4, "Direct Vs Online Sales Comparison Report", "Robin Cosworth", new DateTime(2018, 1, 2), new DateTime(2018, 3, 20)),
new EmployeeTask(17, 4, "Review Sales Report and Approve Modifications", "Kelly Rodriguez", new DateTime(2017, 9, 4), new DateTime(2017, 10, 30)),
new EmployeeTask(18, 4, "Update R&D Strategy", "James Anderson", new DateTime(2017, 11, 13), new DateTime(2017, 12, 4)),
new EmployeeTask(19, 4, "Discuss Updated R&D Strategy", "Antony Remmen", new DateTime(2017, 10, 29), new DateTime(2017, 12, 31)),
new EmployeeTask(20, 4, "Update QA Strategy", "Olivia Peyton", new DateTime(2017, 10, 31), new DateTime(2017, 11, 2)),
new EmployeeTask(21, 4, "Schedule Training Events", "Taylor Riley", new DateTime(2017, 11, 19), new DateTime(2018, 4, 7)),
new EmployeeTask(22, 4, "Approve The Hiring Of John Jeffers", "Amelia Harper", new DateTime(2018, 1, 7), new DateTime(2018, 4, 8)),
new EmployeeTask(23, 0, "Increase Average Subscription Price ", "Wally Hobbs", new DateTime(2017, 8, 9), new DateTime(2017, 9, 13)),
new EmployeeTask(24, 23, "Update Non-Compete Agreements", "Brad Jameson", new DateTime(2017, 8, 9), new DateTime(2017, 9, 3)),
new EmployeeTask(25, 23, "Update Employee Records With New NDA", "Karen Goodson", new DateTime(2017, 8, 23), new DateTime(2018, 9, 10))
};
}
}
// ...
builder.Services.AddSingleton<EmployeeTaskService>();
DxTreeList does not support a template for the pager. However, you can set the PagerVisible property to false to hide the default pager and use custom components for navigation.
In the following code snippet, a custom DxPager component allows users to navigate between pages. An external <div> displays the total number of records next to the pager.
.treelist-container {
width: 950px;
}
.pager-container {
display: flex;
justify-content: space-between;
padding: 8px;
border: 1px solid #d2d2d2;
border-top: none;
}
<div class="treelist-container">
<DxTreeList @ref="@TreeList"
Data="@TreeListData"
KeyFieldName="Id"
ParentKeyFieldName="ParentId"
@bind-PageIndex="@ActivePageIndex"
PagerVisible="false"
PageSize="@PageSize"
FooterDisplayMode="TreeListFooterDisplayMode.Never">
<Columns>
<DxTreeListDataColumn FieldName="Name" Caption="Task" />
<DxTreeListDataColumn FieldName="EmployeeName" />
<DxTreeListDataColumn FieldName="StartDate" />
<DxTreeListDataColumn FieldName="DueDate" />
</Columns>
<TotalSummary>
<DxTreeListSummaryItem SummaryType="TreeListSummaryItemType.Count"
FieldName="@RowCountField"
Visible="false" />
</TotalSummary>
</DxTreeList>
<div class="pager-container">
<DxPager PageCount="@PageCount" @bind-ActivePageIndex="@ActivePageIndex" />
<div>
Total: @TotalRecords records
</div>
</div>
</div>
@code {
ITreeList TreeList { get; set; }
List<EmployeeTask> TreeListData { get; set; }
int PageCount { get; set; }
int TotalRecords { get; set; }
int PageSize { get; set; } = 5;
int ActivePageIndex { get; set; } = 0;
string RowCountField { get; set; } = "DueDate";
protected override void OnInitialized() {
TreeListData = EmployeeTaskService.GenerateData();
}
protected override void OnAfterRender(bool firstRender) {
TotalRecords = (int)(TreeList.GetTotalSummaryValue(TreeList?.GetTotalSummaryItems().First()));
PageCount = (int)Math.Ceiling((decimal)TotalRecords / PageSize);
StateHasChanged();
base.OnAfterRender(firstRender);
}
}
To display an image from a binary source, place an `` element into CellDisplayTemplate and specify the src property. Review the following example:
<DxTreeListDataColumn FieldName="ImageData">
<CellDisplayTemplate>
</CellDisplayTemplate>
</DxTreeListDataColumn>
const string ImageSourceFormat = "data:image/gif;base64,{0}";
void GetImageSource(TreeListCellDisplayTemplateContext context) {
return string.Format(ImageSourceFormat, Convert.ToBase64String((byte[])context.Value));
}
To display links in TreeList cells, implement a CellDisplayTemplate and add a link element to it.
The following code snippet displays links to Wikipedia pages:
@inject SpaceObjectDataProvider SpaceObjectDataProvider
<DxTreeList Data="TreeListData" ChildrenFieldName="Satellites">
<Columns>
<DxTreeListDataColumn FieldName="Name" />
<DxTreeListDataColumn FieldName="TypeOfObject" Caption="Type" />
<DxTreeListDataColumn FieldName="Mass10pow21kg" Caption="Mass, kg" DisplayFormat="N2">
<HeaderCaptionTemplate>Mass, 10<sup>21</sup> × kg</HeaderCaptionTemplate>
</DxTreeListDataColumn>
<DxTreeListDataColumn FieldName="MeanRadiusInKM" Caption="Radius, km" DisplayFormat="N2" />
<DxTreeListDataColumn FieldName="Volume10pow9KM3" DisplayFormat="N2">
<HeaderCaptionTemplate>Volume, 10<sup>9</sup> × km<sup>3</sup></HeaderCaptionTemplate>
</DxTreeListDataColumn>
<DxTreeListDataColumn FieldName="SurfaceGravity" DisplayFormat="N2">
<HeaderCaptionTemplate>Gravity, m/s<sup>2</sup></HeaderCaptionTemplate>
</DxTreeListDataColumn>
<DxTreeListDataColumn FieldName="WikiPage" AllowSort="false" Width="150px">
<CellDisplayTemplate><a href="@context.Value">Open Wikipedia</a></CellDisplayTemplate>
</DxTreeListDataColumn>
</Columns>
</DxTreeList>
@code {
object TreeListData { get; set; }
protected override async Task OnInitializedAsync() {
TreeListData = SpaceObjectDataProvider.GenerateData();
}
}
using System.Collections.Generic;
public class SpaceObject {
public string Name { get; set; }
public string WikiPage { get; set; }
public double MeanRadiusInKM { get; set; }
public double MeanRadiusByEarth { get; set; }
public double Volume10pow9KM3 { get; set; }
public double Mass10pow21kg { get; set; }
public double Density { get; set; }
public double SurfaceGravity { get; set; }
public string TypeOfObject { get; set; }
public List<SpaceObject> Satellites { get; set; }
public SpaceObject(
string name,
string wikiPage,
double meanRadiusInKM,
double meanRadiusByEarth,
double volume10pow9KM3,
double mass10pow21kg,
double density,
double surfaceGravity,
string typeOfObject,
List<SpaceObject> satellites = null
)
{
Name = name;
WikiPage = wikiPage;
MeanRadiusInKM = meanRadiusInKM;
MeanRadiusByEarth = meanRadiusByEarth;
Volume10pow9KM3 = volume10pow9KM3;
Mass10pow21kg = mass10pow21kg;
Density = density;
SurfaceGravity = surfaceGravity;
TypeOfObject = typeOfObject;
Satellites = satellites ?? new List<SpaceObject>();
}
}
public class SpaceObjectDataProvider {
public List<SpaceObject> GenerateData() {
return new List<SpaceObject>() {
new SpaceObject("Sun", "https://en.wikipedia.org/wiki/Sun", 696000, 109.25, 1412000000, 1989100000, 1.409, 274.0, "Star", new List<SpaceObject>() {
new SpaceObject("Mercury", "https://en.wikipedia.org/wiki/Mercury_(planet)", 2439.7, 0.383, 60.83, 330.2, 5.43, 3.7, "Planet"),
new SpaceObject("Venus", "https://en.wikipedia.org/wiki/Venus", 6051.8, 0.950, 928.43, 4868.5, 5.24, 8.872, "Planet"),
new SpaceObject("Earth", "https://en.wikipedia.org/wiki/Earth", 6371.0, 1, 1083.21, 5973.6, 5.515, 9.78033, "Planet", new List<SpaceObject> () {
new SpaceObject("Moon", "https://en.wikipedia.org/wiki/Moon", 1737.1, 0.273, 21.958, 73.5, 3.3464, 1.625, "Satellite")
}),
new SpaceObject("Mars", "https://en.wikipedia.org/wiki/Mars", 3390.0, 0.532, 163.18, 641.85, 3.94, 3.7, "Planet"),
new SpaceObject("Jupiter", "https://en.wikipedia.org/wiki/Jupiter", 69911, 10.97, 1431280, 1898600, 1.33, 24.79, "Planet", new List<SpaceObject>() {
new SpaceObject("Ganymede", "https://en.wikipedia.org/wiki/Ganymede_(moon)", 2631.2, 0.413, 76.30, 148.2, 1.936, 1.428, "Satellite"),
new SpaceObject("Callisto", "https://en.wikipedia.org/wiki/Callisto_(moon)", 2410.3, 0.378, 58.65, 107.6, 1.83, 1.23603, "Satellite"),
new SpaceObject("Io", "https://en.wikipedia.org/wiki/Io_(moon)", 1821.5, 0.286, 25.32, 89.3, 3.528, 1.797, "Satellite"),
new SpaceObject("Europa", "https://en.wikipedia.org/wiki/Europa_(moon)", 1561, 0.245, 15.93, 48, 3.01, 1.316, "Satellite"),
}),
new SpaceObject("Saturn", "https://en.wikipedia.org/wiki/Saturn", 58232, 9.14, 827130, 568460, 0.70, 10.445, "Planet", new List<SpaceObject>() {
new SpaceObject("Titan", "https://en.wikipedia.org/wiki/Titan_(moon)", 2576, 0.404, 71.52, 134.5, 1.88, 1.354, "Satellite"),
new SpaceObject("Rhea", "https://en.wikipedia.org/wiki/Rhea_(moon)", 764.4, 0.12, 1.87, 2.3166, 1.23, 0.26, "Satellite"),
new SpaceObject("Iapetus", "https://en.wikipedia.org/wiki/Iapetus_(moon)", 736, 0.113, 1.55, 1.9739, 1.08, 0.223, "Satellite"),
new SpaceObject("Dione", "https://en.wikipedia.org/wiki/Dione_(moon)", 561.6, 0.088, 0.73, 1.096, 1.48, 0.232, "Satellite"),
new SpaceObject("Tethys", "https://en.wikipedia.org/wiki/Tethys_(moon)", 533, 0.083, 0.624, 0.6173, 1.15, 0.145, "Satellite"),
new SpaceObject("Enceladus", "https://en.wikipedia.org/wiki/Enceladus_(moon)", 252.1, 0.039, 0.067, 0.108, 1.61, 0.111, "Satellite"),
new SpaceObject("Mimas", "https://en.wikipedia.org/wiki/Mimas_(moon)", 198.3, 0.031, 0.033, 0.03749, 1.15, 0.06363, "Satellite")
}),
new SpaceObject("Uranus", "https://en.wikipedia.org/wiki/Uranus", 25362, 3.98, 68340, 86832, 1.30, 8.87, "Planet", new List<SpaceObject>() {
new SpaceObject("Titania", "https://en.wikipedia.org/wiki/Titania_(moon)", 788.9, 0.124, 2.06, 3.526, 1.72, 0.378, "Satellite"),
new SpaceObject("Oberon", "https://en.wikipedia.org/wiki/Oberon_(moon)", 761.4, 0.12, 1.85, 3.014, 1.63, 0.347, "Satellite"),
new SpaceObject("Umbriel", "https://en.wikipedia.org/wiki/Umbriel_(moon)", 584.7, 0.092, 0.84, 1.2, 1.4, 0.234, "Satellite"),
new SpaceObject("Ariel", "https://en.wikipedia.org/wiki/Ariel_(moon)", 578.9, 0.091, 0.81, 1.35, 1.67, 0.269, "Satellite"),
new SpaceObject("Miranda", "https://en.wikipedia.org/wiki/Miranda_(moon)", 235.8, 0.037, 0.055, 0.0659, 1.20, 0.07910375, "Satellite"),
}),
new SpaceObject("Neptune", "https://en.wikipedia.org/wiki/Neptune", 24622, 3.87, 62540, 102430, 1.76, 11.15, "Planet", new List<SpaceObject>() {
new SpaceObject("Triton", "https://en.wikipedia.org/wiki/Triton_(moon)", 1353.4, 0.212, 10.38, 21.5, 2.061, 0.782, "Satellite"),
new SpaceObject("Proteus", "https://en.wikipedia.org/wiki/Proteus_(moon)", 210, 0.033, 0.038, 0.050, 1.3, 0.0666, "Satellite"),
}),
new SpaceObject("Eris", "https://en.wikipedia.org/wiki/Eris_(dwarf_planet)", 1170, 0.182, 7, 16.7, 2.25, 0.662, "Dwarf planet"),
new SpaceObject("Pluto", "https://en.wikipedia.org/wiki/Pluto", 1153, 0.181, 7.15, 13.105, 2.0, 0.61, "Dwarf planet"),
new SpaceObject("Makemake", "https://en.wikipedia.org/wiki/Makemake_(dwarf_planet)", 710, 0.126, 1.8, 3, 2.0, 0.4, "Dwarf planet"),
new SpaceObject("Haumea", "https://en.wikipedia.org/wiki/Haumea_(dwarf_planet)", 575, 0.117, 1.3, 4.006, 3, 0.44, "Dwarf planet"),
new SpaceObject("Ceres", "https://en.wikipedia.org/wiki/Ceres_(dwarf_planet)", 475, 0.076, 0.437, 0.95, 2.08, 0.27, "Dwarf planet"),
new SpaceObject("Pallas", "https://en.wikipedia.org/wiki/2_Pallas", 266, 0.0042, 0.078, 0.211, 2.8, 0.2, "Asteroid"),
new SpaceObject("Vesta", "https://en.wikipedia.org/wiki/4_Vesta", 264.6, 0.0042, 0.078, 0.262, 3.42, 0.251, "Asteroid")
})
};
}
}
// ...
builder.Services.AddSingleton<SpaceObjectDataProvider>();