How does a user control bind a template element of an element to a user property?

I have a simple user control that is essentially just an auto-computer with some user logic.

For a specific instance (collection of faces) I want it to look like this:

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

However, I want the data source to be shared, and so the display values ​​will be different (ValueMemberBinding and text TextBlock of the template). This is why I am creating a custom control, so I can point out the differences with the properties.

I have no problem setting the source using the user control property, but I am having difficulty with the display binding properties. Right now I have:

 public static DependencyProperty DisplayMemberProperty = DependencyProperty.Register("DisplayMember", typeof(string), typeof(myAutoComplete), null); public string DisplayMember { get { return myACB.ValueMemberPath; } set { myACB.ValueMemberPath = value; // this works fine // but how can set the text binding for the templated textblock? } } 

I want the DisplayMember property to be the property name displayed for any type of user collection (faces, cars, etc.). I got attached to autocomplete.

I do not think that I can modify the datatemplate programmatically. Is there a way to do this with binding (relative source)?

+4
source share
3 answers

Thank you for your suggestions.

I was unable to find the solution that I preferred, but my workaround is to simply pass the datatemplate resource as a property and assigned to its autocompletebox element.

Define a template:

 <DataTemplate x:Key="myCustomDT"> <!-- whatever you want here --> </DataTemplate> 

Create a user management property for it:

 public static DependencyProperty DisplayTemplateProperty = DependencyProperty.Register("DisplayTemplate", typeof(DataTemplate), typeof(myAutoComplete), null); public DataTemplate DisplayTemplate { get { return myACB.ItemTemplate; } set { myACB.ItemTemplate = value; } } 

Now:

 <local:myAutoComplete DisplayTemplate="{StaticResource myCustomDT}" /> 

Not the best method, but it will work for now.

0
source

I'm not sure if this works, but I think you can bind the text directly to the ValueMemberBinding property and use the converter to get the text from it ...

+2
source
 <TextBlock Text="{TemplateBinding DisplayMember}" /> 
0
source

All Articles