How to create a DataTemplate with code?
DataTemplate does not have a VisualTree property like in WPF.
The docu doesn't help either.
There is an IDataTemplateController, but it does not control anything. <ignorable>weird MAUI times again</ignorable>.
Found it in source code: There is a constructor parameter for a function which creates the view.
/// <Docs>
/// <param name="loadTemplate">A custom content generator to be called </param>
/// <summary>Creates and initializes a new instance of the <see cref="T:Microsoft.Maui.Controls.DataTemplate" /> class.</summary>
/// <remarks>To be added.</remarks>
/// </Docs>
public DataTemplate(Func<object> loadTemplate);
new DataTemplate(() => {
var label = new Label();
label.SetBinding(Label.TextProperty, new Binding("."));
return label;
});
There is currently a bug in Binding and you have to specify ".", a fix is merged.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With