C# 对于用户控件,如何将项模板项绑定到用户属性?
我有一个简单的用户控件,它本质上只是一个带有一些自定义逻辑的自动完成框 对于一个特定实例(一组人员),我希望它如下所示:C# 对于用户控件,如何将项模板项绑定到用户属性?,c#,wpf,vb.net,silverlight,binding,C#,Wpf,Vb.net,Silverlight,Binding,我有一个简单的用户控件,它本质上只是一个带有一些自定义逻辑的自动完成框 对于一个特定实例(一组人员),我希望它如下所示: <sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding People}" FilterMode="StartsWith" MinimumPrefixLength="2" ValueMemberBinding={Binding LastName}> <sdk:AutoCompleteBox.ItemT
<sdk:AutoCompleteBox Name="myACB" ItemsSource="{Binding People}" FilterMode="StartsWith" MinimumPrefixLength="2" ValueMemberBinding={Binding LastName}>
<sdk:AutoCompleteBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</sdk:AutoCompleteBox.ItemTemplate>
</sdk:AutoCompleteBox>
我希望DisplayMember属性是我绑定到AutoCompleteBox的任何类型的自定义集合(个人、汽车等)的属性名称
我认为我无法以编程方式修改datatemplate。有没有一种方法可以通过绑定(相对源)实现这一点?我不确定这是否有效,但我认为您可以将文本直接绑定到ValueMemberBinding属性,并使用转换器从中提取文本…
<TextBlock Text="{TemplateBinding DisplayMember}" />
谢谢您的建议
我无法获得我喜欢的解决方案,但我的解决方法是将datatemplate资源作为属性传入,并将其分配给autocompletebox itemtemplate
定义模板:
<DataTemplate x:Key="myCustomDT">
<!-- whatever you want here -->
</DataTemplate>
现在:
这不是最好的方法,但它现在会起作用
<DataTemplate x:Key="myCustomDT">
<!-- whatever you want here -->
</DataTemplate>
public static DependencyProperty DisplayTemplateProperty = DependencyProperty.Register("DisplayTemplate", typeof(DataTemplate), typeof(myAutoComplete), null);
public DataTemplate DisplayTemplate {
get { return myACB.ItemTemplate; }
set { myACB.ItemTemplate = value; }
}
<local:myAutoComplete DisplayTemplate="{StaticResource myCustomDT}" />