Install TextSearch.Text for WPF-ComboBoxItem using DataTemplate

I use a datatemplate to render some elements in a ComboBox, ItemsSource is bound to an ObservableCollection. To keep it simple, let me say that I put people in an 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>

ComboBox, person. , TextSearch.TextPath ComboBox, TextSearch.Text-Property ComboBoxItem DataTemplate. , ( MultiBinding StringFormat, Snoop), StackPanel, , Snoop ( ), , , ComboBoxItemTemplate, .. , , ComboBoxItem StackPanel. TextSearch.Text , ComboBoxItem, - .

:. TextSearch.Text-Property DataTemplate ComboBoxItem, XAML-Styles -Control-Templates? ControlTemplates ComboBox ComboBoxItem (Item-) DataTemplate, -Behind , , . , . , , TemplateBinding RelativeSource-stuff... , , - , . , "" , " " , .

?

+5
2

, , . TextSearch.Text . , ItemContainerStyle :

<ComboBox.ItemContainerStyle>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="TextSearch.Text">
            <Setter.Value>
                <MultiBinding StringFormat="{} {0} {1}">
                    <Binding Path="FirstName"/>
                    <Binding Path="LastName"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ComboBox.ItemContainerStyle>
+1

: , , XAML, .

: ComboBox TextSearch.Text , Items ItemsSource. , , , .

, TextSearch, , TextSearch.Text , ComboBox.Items. , Person DependencyObject, , .

:

Person, ToString(), , Fullname, Textsearch.TextPath ComboBox. :

public class Person
{
     string FirstName { get; set; }
     string LastName {get; set; }
     string FullName { get { return String.Format("{0} {1}", FirstName, LastName); } }
}

<ComboBox TextSearch.TextPath="FullName" ItemsSource="collectionOfPersons"/>

Person, -, .

+8

All Articles