Articles in this section
Category / Section

How to apply cell and edit template based on row index in WPF TreeGrid?

3 mins read

Typically, in a template selector, the cell and edit templates are loaded based on the value of the underlying data object. With the customization below, we can load templates based on the row index of the SfTreeGrid. To achieve this, we’ve defined a static property for the SfTreeGrid in both selector classes and assigned the TreeGrid instance within the MainWindow class.

XAML code:

<Window.Resources>
   <local:CustomCellTemplateSelector x:Key="cellTemplateSelector" />
   <local:CustomEditTemplateSelector x:Key="editTemplateSelector"/>

   <DataTemplate x:Key="ComboBoxCellTemplate">
       <Grid>
           <syncfusion:ComboBoxAdv BorderBrush="Transparent" Background="Transparent" Grid.Column="1" 
                                   SelectedItem="{Binding SelectedComboItem}"
           ItemsSource="{Binding ComboBoxItems}" syncfusion:VisualContainer.WantsMouseInput="False"
           syncfusion:FocusManagerHelper.FocusedElement="True" />
       </Grid>
   </DataTemplate>

   <DataTemplate x:Key="TextBlockCellTemplate">
       <TextBlock VerticalAlignment="Center"
   Text="{Binding Path=FirstName}"/>
   </DataTemplate>
   
   <DataTemplate x:Key="ComboBoxEditTemplate">
       <Grid>
           <syncfusion:ComboBoxAdv BorderBrush="Transparent" Background="Transparent" Grid.Column="1" 
                                   SelectedItem="{Binding SelectedComboItem}"
                           ItemsSource="{Binding ComboBoxItems}" syncfusion:VisualContainer.WantsMouseInput="False"
                           syncfusion:FocusManagerHelper.FocusedElement="True" />
       </Grid>
   </DataTemplate>

   <DataTemplate x:Key="TextBoxEditTemplate">
       <TextBox Height="20"
   VerticalAlignment="Center" syncfusion:FocusManagerHelper.FocusedElement="True"
   Text="{Binding Path=FirstName}" />
   </DataTemplate>
</Window.Resources>

C# code:

public MainWindow()
{
   InitializeComponent();
   CustomCellTemplateSelector.SfTreeGridCell = this.treeGrid;
   CustomEditTemplateSelector.SfTreeGridEdit = this.treeGrid;
}

public class CustomEditTemplateSelector : DataTemplateSelector
{
   public static SfTreeGrid SfTreeGridEdit { get; set; }
   public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
   {

       if (item == null)
           return null;
       var data = item as EmployeeInfo;
       var recordRowIndex = (SfTreeGridEdit.DataContext as ViewModel).Employees.IndexOf(data);
       var rowIndex = SfTreeGridEdit.ResolveToRowIndex(recordRowIndex);
       if (rowIndex == 1 || rowIndex == 3)
           return Application.Current.MainWindow.FindResource("ComboBoxEditTemplate") as DataTemplate;

       else
           return Application.Current.MainWindow.FindResource("TextBoxEditTemplate") as DataTemplate;
   }
}

public class CustomCellTemplateSelector : DataTemplateSelector
{
   public static SfTreeGrid SfTreeGridCell { get; set; }
   public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
   {
       if (item == null)
           return null;
       var data = item as EmployeeInfo;
       var recordRowIndex = (SfTreeGridCell.DataContext as ViewModel).Employees.IndexOf(data);
       var rowIndex = SfTreeGridCell.ResolveToRowIndex(recordRowIndex);
       if (rowIndex == 1 || rowIndex == 3)
           return Application.Current.MainWindow.FindResource("ComboBoxCellTemplate") as DataTemplate;

       else
           return Application.Current.MainWindow.FindResource("TextBlockCellTemplate") as DataTemplate;
   }
}

Image reference:

image.png

View the sample in GitHub

Take a moment to peruse the WPF-SfTreeGrid Template Selector Documentation, to learn more about template selector with examples.

Conclusion

I hope you enjoyed learning about how to apply cell and edit template based on row index in WPF TreeGrid.

You can refer to our WPF TreeGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied