The parameterized construction of GUI elements is possible using ObjectDataProvider
but dynamic parameter values are not allowed e.g. using the data template below in an ItemsControl.ItemTemplate
is not possible if you expect to pass on items specific parameter value! ...
XAML:
<Window x:Class="LDAPAutocomplete.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LDAPAutocomplete"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="MyContentTemplate">
<DataTemplate.Resources>
<ObjectDataProvider x:Key="MyUserControlProvider"
ObjectType="{x:Type local:MyButton}">
<ObjectDataProvider.ConstructorParameters>
<System:String>Test Me</System:String>
</ObjectDataProvider.ConstructorParameters>
</ObjectDataProvider>
</DataTemplate.Resources>
<ContentPresenter
Content="{Binding
Source={StaticResource MyUserControlProvider}}" />
</DataTemplate>
</Window.Resources>
<StackPanel>
<local:LDAPAutocompleteTextBox/>
<ContentControl
ContentTemplate="{StaticResource MyContentTemplate}"
Content="123"/>
</StackPanel>
</Window>
Code Behind:
public class MyButton : Button
{
public MyButton(string content)
{
this.Content = content;
}
}