可以使用 GUI 元素的参数化构造,ObjectDataProvider
但不允许使用动态参数值,ItemsControl.ItemTemplate
例如,如果您希望传递项目特定的参数值,则无法使用下面的数据模板!...
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>
代码背后:
public class MyButton : Button
{
public MyButton(string content)
{
this.Content = content;
}
}