user2519971
user2519971

Reputation: 345

Searching in WPF comboBox showing two columns

I have to enable searching in WPF comboBox showing two columns in my WPF MVVM application.

Below is my code which is showing two columns like : First Name - Last Name

    <ComboBox Grid.Column="3" Grid.Row="15" Height="Auto" HorizontalAlignment="Stretch" 
                Name="cmbName" VerticalAlignment="Stretch"
                SelectedItem="{Binding Name, Mode=TwoWay}"
                ItemsSource="{Binding GetAllName}"
                IsTextSearchEnabled="True">

        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} - {1}">
                                <Binding Path="FirstName" />
                                <Binding Path="LastName" />
                            </MultiBinding>
                        </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

I think in this scenario IsTextSearchEnabled is not playing any role.

Any help on this ?

Upvotes: 4

Views: 5460

Answers (2)

Hiệp L&#234;
Hiệp L&#234;

Reputation: 634

You can use TextSearch.TextPath in this case.

<ComboBox Grid.Column="3" Grid.Row="15" Height="Auto" HorizontalAlignment="Stretch" 
                Name="cmbName" VerticalAlignment="Stretch"
                SelectedItem="{Binding Name, Mode=TwoWay}"
                ItemsSource="{Binding GetAllName}">
        <TextSearch.TextPath>FirstName</TextSearch.TextPath>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock DataContext="{Binding}">
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0} - {1}">
                                <Binding Path="FirstName" />
                                <Binding Path="LastName" />
                            </MultiBinding>
                        </TextBlock.Text>
                </TextBlock>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

Upvotes: 6

Sheridan
Sheridan

Reputation: 69959

You seem to be somewhat confused. Firstly, you are not displaying two columns in your ComboBox, you are displaying two field values... maybe you should edit your misleading title?

Secondly, I don't believe that you fully understand the use of the IsTextSearchEnabled property. You are however correct that using this property as you have is achieving nothing. From the ItemsControl.IsTextSearchEnabled Property page on MSDN:

Gets or sets a value that indicates whether TextSearch is enabled on the ItemsControl instance.

From the TextSearch Class page on MSDN:

This class is used to assign a string to items in a control's collection. Assigning a string to each item in the collection accomplishes two objectives. It specifies the text to display when the item is selected, and it enables the user to select an item by typing the assigned string.

For example, assume that a ComboBox contains a collection of Image objects, one of which is an image of a dog. If you assign the string, "Dog" to that item, the user can select the dog by typing the word in the combo box's text box. As soon as the user types enough of the word to distinguish it from other items in the selection, the image of the dog will be selected. If IsEditable is set to true on the ComboBox, "Dog" will appear in the text box.

You can specify the text that identifies an item by using the TextSearch.TextPath property on a control or by setting the Text property on each item in the control's collection. Setting one of these properties ensures that unexpected text is not displayed. If you set the Text property on a control's collection item, the TextPath property will be ignored. If you set the TextPath property to a value that is not the name of an actual property, TextPath is ignored.

Upvotes: -2

Related Questions