Pogrammatically Add Item Template to TreeView

Introduction

This blog gives an illustration for adding item controls to tree view at run time, here my tree view name is filterableTreeview. to add item template into tree view i have taken of two classes

  1. FrameworkElementFactory : fore create data template
  2. Binding : helps to bind value with template
  3. DataTemplate : Helps to bind value with template

See the bellow code your will get some ideas.

FrameworkElementFactory FEF = new FrameworkElementFactory(typeof(Label));

Binding CBG = new Binding("object to bind");

FEF.SetValue(Label.ContentProperty, CBG);

FEF.SetValue(Label.HorizontalAlignmentProperty, HorizontalAlignment.Stretch);

FEF.SetValue(Label.VerticalAlignmentProperty, VerticalAlignment.Stretch);

DataTemplate IT = new DataTemplate();

IT.VisualTree = FEF;

this.filterableTreeview.ItemTemplate = IT;

Ebook Download
View all
Learn
View all