I use a datatemplate to visualize some items in a ComboBox, the ItemsSource is bound to an ObservableCollection. To keep it simple, let's say I put persons into the ObservableCollection:
public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
}
My DataTemplate looks like this:
<DataTemplate TargetType="{x:Type Person}">
<StackPanel Orientation="Horizontal">
<TextSearch.Text>
<MultiBinding StringFormat="{} {0} {1}">
<Binding Path="FirstName"/>
<Binding Path="LastName"/>
</MultiBinding>
</TextSearch.Text>
<TextBlock Text="{Binding FirstName}" Margin="2,0" />
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
Now I want to enable autocompletion for full names in the ComboBox without introducing a third property on my person class. Hence I do not want to use TextSearch.TextPath Property on the ComboBox, but instead I would like to bind the TextSearch.Text-Property of each ComboBoxItem in the DataTemplate. Unfortunately, when I do this (which works with a MultiBinding and StringFormat, tested with Snoop), the bound value is registered for my StackPanel only, but using Snoop (great tool) I found that this stackpanel serves just as content for some further ComboBoxItemTemplate, which puts another border etc. and finally a ComboBoxItem-tag around my outer StackPanel. Therefore, the TextSearch.Text-setting is not effective, because it must be set in the created ComboBoxItem, and not somewhere within.
Question now: How can I propagate this TextSearch.Text-Property from my DataTemplate to the surrounding ComboBoxItem using XAML-Styles and -Control-Templates only? The solution may modify the default-ControlTemplates of ComboBox and ComboBoxItem and my custom (Item-)DataTemplate, but not use any Code-Behind, or at least not too much. Maybe an attached behaviour would be ok, too. But I'm almost sure there must be a way to make it work without, TemplateBinding or RelativeSource-stuff... And of course the solution must make my keyboard-selection and textcompletion work, s.th. when the list contains Hans Josef and Hans Peter, then entering 'Hans ' should autosuggest Hans Josef, whereas entering 'Hans P' quickly enough should autosuggest Hans Peter.
Any solutions?