How to create DataTemplate in code?

Emon Haque 3,176 Reputation points
2021-05-03T07:23:43.29+00:00

I've seen some example of creating DataTemplate with StackPanel and deprecated FrameworkElementFactory. For the Tenant under Lease section I want to have a DataTemplate with a Grid with two columns and two TextBlocks (one for Name and the other for Phone):

93159-test.gif

The SelectItem, tenant, is my control and want the following dataTemplate Grid to be its ItemTemplate

var nameBlock = new TextBlock();  
var phoneBlock = new TextBlock();  
Grid.SetColumn(phoneBlock, 1);  
var dataTemplate= new Grid() {  
    ColumnDefinitions = {  
        new ColumnDefinition(),  
        new ColumnDefinition(){Width = GridLength.Auto}  
    },  
    Children = {nameBlock, phoneBlock}  
};  
          
tenant = new SelectItem() {  
    Hint = "Tenant",  
    IsRequired = true,  
    Icon = Icons.Tenant,  
    SelectedValuePath = nameof(Tenant.Id),  
    SearchPath = nameof(Tenant.Name),  
    DisplayPath = nameof(Tenant.Name),  
    ItemTemplate = dataTemplate  
};  

this way it doesn't work!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,775 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Emon Haque 3,176 Reputation points
    2021-05-03T13:32:37.427+00:00

    The deprecated method still works:

    var gridFactory = new FrameworkElementFactory(typeof(Grid));
    var column1Factory = new FrameworkElementFactory(typeof(ColumnDefinition));
    var column2Factory = new FrameworkElementFactory(typeof(ColumnDefinition));
    var nameFactory = new FrameworkElementFactory(typeof(TextBlock));
    var phoneFactory = new FrameworkElementFactory(typeof(TextBlock));
    
    column1Factory.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Star));
    column2Factory.SetValue(ColumnDefinition.WidthProperty, new GridLength(1, GridUnitType.Auto));
    phoneFactory.SetValue(Grid.ColumnProperty, 1);
    
    nameFactory.SetBinding(TextBlock.TextProperty, new Binding("Name"));
    phoneFactory.SetBinding(TextBlock.TextProperty, new Binding("ContactNo"));
    
    gridFactory.AppendChild(column1Factory);
    gridFactory.AppendChild(column2Factory);
    gridFactory.AppendChild(nameFactory);
    gridFactory.AppendChild(phoneFactory);
    
    tenant = new SelectItem() {
        Hint = "Tenant",
        IsRequired = true,
        Icon = Icons.Tenant,
        SelectedValuePath = nameof(Tenant.Id),
        SearchPath = nameof(Tenant.Name),
        //DisplayPath = nameof(Tenant.Name)
        ItemTemplate = new DataTemplate() { VisualTree = gridFactory }
    };
    

    BUT boring! It'd be nice if I could do like this:

    var nameBlock = new TextBlock();
    var phoneBlock = new TextBlock();
    Grid.SetColumn(phoneBlock, 1);
    var template = new DataTemplate(){ new Grid() {
        ColumnDefinitions = {
            new ColumnDefinition(),
            new ColumnDefinition(){Width = GridLength.Auto}
        },
        Children = { nameBlock, phoneBlock }
    };
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.