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 }
};