I have below combo box in mvvm-wpf application. I need to implement “Text search” in this..(along with multibinding). Can anybody help me please.
<StackPanel Orientation="Horizontal">
<TextBlock Text="Bid Service Cat ID"
Margin="2"></TextBlock>
<ComboBox Width="200"
Height="20"
SelectedValuePath="BidServiceCategoryId"
SelectedValue="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Path=DataContext.SelectedBidServiceCategoryId.Value}"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Path=DataContext.BenefitCategoryList}"
Margin="12,0">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="BidServiceCategoryId" />
<Binding Path="BidServiceCategoryName" />
</MultiBinding>
</TextBlock.Text></TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
Unfortunately,
TextSearch.Textdoesn’t work in a DataTemplate. Otherwise you could have done something like thisHowever this won’t work, so I see two solutions to your problem.
First way
You set
IsTextSearchEnabledtoTruefor theComboBox, overrideToStringin your source class and change theMultiBindingin theTextBlockto aBindingXaml
Source class
Second Way
If you don’t want to override ToString I think you’ll have to introduce a new Property in your source class where you combine
BidServiceCategoryIdandBidServiceCategoryNamefor theTextSearch.TextPath. In this example I call it BidServiceCategory. For this to work, you’ll have to callOnPropertyChanged("BidServiceCategory");whenBidServiceCategoryIdorBidServiceCategoryNamechanges as well. If they are normal CLR properties, you can do this inset, and if they are dependency properties you’ll have to use the property changed callbackXaml
Source class