Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 905
user2519971 Avatar asked Oct 23 '25 18:10

user2519971


1 Answers

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>
like image 116
Hiệp Lê Avatar answered Oct 25 '25 09:10

Hiệp Lê