WPF repeater control - wpf

I have the following working code:
<StackPanel>
<TextBlock FontSize="14" Foreground="White" Text="Case Type: " TextDecorations="Underline"/>
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeA}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeA}}" />
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeB}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeB}}" />
...
...
...
<RadioButton IsChecked="{Binding CaseType, Converter={StaticResource MyEnumToBooleanConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeJ}}"
Style="{StaticResource ToggleButtonStyle}"
Content="{Binding CaseType, Converter={StaticResource MyEnumDescriptionConverter}, ConverterParameter={x:Static order:CaseTypeEnum.TypeJ}}" />
</StackPanel>
Is there any way to do the same functionality without copy/paste :)

Ok without knowing your logic I cannot validate if you actually need two values going into the converter where 1 is the same for every item anyways.
However assuming you indeed do need them:
xaml:
<StackPanel>
<ItemsControl ItemsSource="{Binding Items}">
<ItemsControl.Resources>
<local:MyEnumDescriptionConverter x:Key="MyEnumDescriptionConverter" />
<local:MyEnumToBooleanConverter x:Key="MyEnumToBooleanConverter" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<RadioButton>
<RadioButton.Content>
<MultiBinding Converter="{StaticResource MyEnumDescriptionConverter}">
<Binding Path="." />
<Binding Path="DataContext.CaseType"
RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}" />
</MultiBinding>
</RadioButton.Content>
<RadioButton.IsChecked>
<MultiBinding Converter="{StaticResource MyEnumToBooleanConverter}">
<Binding Path="." />
<Binding Path="DataContext.CaseType"
RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type ItemsControl}}" />
</MultiBinding>
</RadioButton.IsChecked>
</RadioButton>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
Starting from the Top:
Items is defined as:
public List<CaseTypeEnum> Items {
get {
return Enum.GetValues(typeof(CaseTypeEnum)).Cast<CaseTypeEnum>().ToList();
}
}
and
private CaseTypeEnum _caseType;
public CaseTypeEnum CaseType {
get {
return _caseType;
}
set {
if (value == _caseType)
return;
_caseType = value;
RaisePropertyChanged(() => CaseType);
}
}
enum:
public enum CaseTypeEnum{
TypeA,
TypeB,
TypeC,
TypeD,
TypeE,
TypeF,
TypeG,
TypeH,
TypeI,
TypeJ,
}
As for the two MultiBinding's, I just put some dummy code like
MyEnumDescriptionConverter -
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length < 2)
return string.Empty;
return string.Format("Formatted {0} with CaseType property: {1}", (CaseTypeEnum)values[0], (CaseTypeEnum)values[1]);
}
and MyEnumToBooleanConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
if (values.Length < 2)
return false;
return ((CaseTypeEnum)values[0]).ToString().EndsWith("D");
}
which should when run give you:
You can download the sample Here

Related

Multiple Command parameter values are not getting passed to view-model during multi-binding

I've nested menuitems bounded to observable collection named 'CollectionOfAuthors'.
Here's the MenuItem Hierarchy:
Author -->AuthorName1-->BookName1,BookName2,BookName3
Author is TopLevelMenuItem which opens in list of Author names such that each Author name opens into list of Books.
While Clicking on each BookName menuitem through NavigateToBook command, I want to send the BookName, AuthorName and AuthorID to ViewModel as command parameters,
But I am finding empty values as (DependencyProperty.UnsetValue) passed to ViewModel.
Need to know what correction is required?
View.xaml
<Menu>
<MenuItem Header="Authors" x:Name="TopLevelMenuItem"
ItemsSource="{Binding CollectionOfAuthors, Mode=TwoWay}">
<in:Interaction.Triggers>
<in:EventTrigger EventName="PreviewMouseLeftButtonDown">
<in:InvokeCommandAction Command="{Binding DataContext.RefreshAuthorsList,RelativeSource={RelativeSource AncestorType=Menu}}"/>
</in:EventTrigger>
</in:Interaction.Triggers>
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Books}">
<StackPanel>
<TextBlock x:Name="tbAuthor" Text="{Binding AuthorName}"/>
<TextBlock x:Name="tbAuthorID" Text="{Binding AuthorID}" Visibility="Collapsed"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="tbBookName" Text="{Binding}">
<TextBlock.InputBindings>
<MouseBinding Command="{Binding DataContext.NavigateToBook, RelativeSource={RelativeSource AncestorType=Menu}}" MouseAction="LeftClick" >
<MouseBinding.CommandParameter>
<MultiBinding Converter="{StaticResource MultiCommandConverter}">
<Binding Path="Text" ElementName="tbBookName"/>
<Binding Path="DataContext.AuthorName" RelativeSource="{RelativeSource AncestorLevel=2, AncestorType=MenuItem}" />
<Binding Path="DataContext.AuthorID" RelativeSource="{RelativeSource AncestorLevel=2, AncestorType=MenuItem}" />
</MultiBinding>
</MouseBinding.CommandParameter>
</MouseBinding>
</TextBlock.InputBindings>
</TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</Menu>
ViewModel.cs
public ICommand NavigateToBook
{
get { return new DelegateCommand(NavigateToBookExecute); }
}
private void NavigateToBookExecute(object obj)
{
string selectedBookName = ((object[])obj)[0].ToString();
string selectedAuthorName = ((object[])obj)[1].ToString();
string selectedAuhorID = ((object[])obj)[2].ToString();
}
public ICommand RefreshAuthorsList
{
get { return new DelegateCommand(RefreshAuthorsListExecute); }
}
private void RefreshAuthorsListExecute(object m)
{
CollectionOfAuthors = new ObservableCollection<Author>();
//Here AuthorDetails is another global collection which gets loaded during constructor call
foreach (var objAuthorItem in AuthorDetails)
{
CollectionOfAuthors.Add(new Author
{
AuthorName = objAuthorItem.DisplayName,
Books = objAuthorItem.ListOfBooks,
AuthorID = objAuthorItem.Id,
});
}
}
private ObservableCollection<Author> _collectionOfAuthors;
public ObservableCollection<Author> CollectionOfAuthors
{
get { return _collectionOfAuthors; }
set { SetProperty(ref _collectionOfAuthors, value); }
}
Author.cs
public class Author
{
public string AuthorName { get; set; }
public string AuthorID { get; set; }
List<string>Books = new List<string>();
}
MultiCommandConverter.cs
public class MultiCommandConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.Clone();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Since you have Command at top level menu item, this Command will try to call even before your inner Command, no mather what event should trigger it.
As Workaround you could pass IsSubmenuOpen property of TopMenuItem as CommandParameter, and check if Menu is opened, and then in Command's execute action you could check if menu is Opened then continue or return. This will stop your items from being refreshed.
CallStack of your command is:
Click on book menuItem
RefreshListCommand runs
items are being refreshed, old ones are removed
Binding is trying to get properites from just removed items
Sample solution:
View.xaml
<Menu>
<MenuItem Header="Authors" Background="Red" x:Name="TopLevelMenuItem"
ItemsSource="{Binding CollectionOfAuthors, Mode=TwoWay}">
<in:Interaction.Triggers>
<in:EventTrigger EventName="PreviewMouseLeftButtonDown">
<in:InvokeCommandAction Command="{Binding DataContext.RefreshAuthorsList,RelativeSource={RelativeSource AncestorType=Menu}}" CommandParameter="{Binding IsSubmenuOpen , ElementName=TopLevelMenuItem}"/>
</in:EventTrigger>
</in:Interaction.Triggers>
<MenuItem.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Path=Books}">
<StackPanel DataContext="{Binding}">
<TextBlock x:Name="tbAuthor" Text="{Binding AuthorName}"/>
<TextBlock x:Name="tbAuthorID" Text="{Binding AuthorID}" Visibility="Collapsed"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock x:Name="tbBookName" DataContext="{Binding}" Text="{Binding}">
<in:Interaction.Triggers>
<in:EventTrigger EventName="MouseDown">
<in:InvokeCommandAction Command="{Binding DataContext.NavigateToBook, RelativeSource={RelativeSource AncestorType=Menu}}" >
<in:InvokeCommandAction.CommandParameter>
<MultiBinding Converter="{StaticResource MultiCommandConverter}">
<Binding Path="Text" ElementName="tbBookName"/>
<Binding Path="DataContext.AuthorName" RelativeSource="{RelativeSource AncestorLevel=1, AncestorType=StackPanel}" />
<Binding Path="DataContext.AuthorID" RelativeSource="{RelativeSource AncestorLevel=1, AncestorType=StackPanel}" />
</MultiBinding>
</in:InvokeCommandAction.CommandParameter>
</in:InvokeCommandAction>
</in:EventTrigger>
</in:Interaction.Triggers>
</TextBlock>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</MenuItem.ItemTemplate>
</MenuItem>
</Menu>
And then in your RefreshAuthorsListExecute
private void RefreshAuthorsListExecuteExecute(object m)
{
if ((bool)m)
return;

WPF DataGrid ClipboardBinding Multibinding possible?

is there a solution to bind multiple properties to my ClipboardBinding.
I tried the following code but this didnt work:
<DataGridTemplateColumn CanUserSort="True" SortMemberPath="Characteristic.Area.Name.ActualTranslation" MinWidth="120" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="5,0,5,0">
<TextBlock Text="{Binding Characteristic.Area.Name.ActualTranslation}"></TextBlock>
<TextBlock Text=" "></TextBlock>
<TextBlock Text="{Binding AreaItem.Value}"></TextBlock>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{lex:Loc Area}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.ClipboardContentBinding>
<!-- TODO: ClipboardBinding Area -->
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Characteristic.Area.Name.ActualTranslation" />
<Binding Path="AreaItem.Value" />
</MultiBinding>
</DataGridTemplateColumn.ClipboardContentBinding>
</DataGridTemplateColumn>
i'd appreciate adivce for a workaround too.
Please help
You should use converter (msdn).
class StringFormatConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return string.Format(parameter.ToString(), values);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML:
<DataGridTemplateColumn.ClipboardContentBinding>
<MultiBinding
ConverterParameter=" {0} {1}"
Converter="{StaticResource conString}">
<Binding Path="Characteristic.Area.Name.ActualTranslation" />
<Binding Path="AreaItem.Value" />
</MultiBinding>
</DataGridTemplateColumn.ClipboardContentBinding>

How to Bind on a Property by String as Propertyname

Hi i want to Bind to an "unknown" (i only get a string) Property in Xaml
at first i wrote an IValueConverter but you can't bind to ConverterParameter
so i rewrite it as IMultiValueConverter but now i'm unable to figure out how to use the <Binding /> with out Path
or my i'm wrong?
if you write <TextBlock Text="{Binding}" /> you will get the object Person
and with {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=View.Columns[0].Header}} i'm able to access the Header Text of the first row
now i'm only need to combine both and a will get the Property right?
my test Xaml code:
<UserControl x:Class="Frameworktest.View.auswahl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Frameworktest">
<UserControl.Resources>
<local:toPropertyConverter x:Key="PropertyConverter"/>
</UserControl.Resources>
<StackPanel>
<!--...-->
<Border BorderThickness="5" HorizontalAlignment="Left" VerticalAlignment="Top"
BorderBrush="Green" CornerRadius="5">
<ListView Name="listView1" IsSynchronizedWithCurrentItem="False"
ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}" <!--ObservableCollection<Person>-->
SelectedItem="{Binding selectedItem, UpdateSourceTrigger=PropertyChanged}">
<ListView.View>
<GridView>
<GridViewColumn Header="Name">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="1">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource PropertyConverter}">
<Binding /><!-- How do i {Binding} here?-->
<Binding Source="{Binding RelativeSource={Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}, Path=View.Columns[0].Header}}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Firstname" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="1" Text="{Binding Path=Name}" Width="100"/><!--works-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Age">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Margin="1" Text="{Binding Age}" Width="50"/><!--works-->
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</Border>
</StackPanel>
</UserControl>
the Converter:
public class toPropertyConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values[0].GetType().GetProperty((string)values[1]);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
The Model
public class Person : MBase, IContains
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value;
RaisePropertyChanged(() => Reg(() => Name));
}
}
private string _firstname;
public string Firstname
{
get { return _firstname; }
set
{
_firstname = value;
RaisePropertyChanged(() => Reg(() => Firstname));
}
}
private int _age;
public int Age
{
get { return _age; }
set
{
_age = value;
RaisePropertyChanged(() => Reg(() => Age));
}
}
public bool Contains(string text)
{
string pers = string.Format("{0} {1}", Firstname, Name);
return pers.Contains(text);
}
}
Update my current Multibindung
<MultiBinding Converter="{StaticResource PropertyConverter}">
<Binding Path="."/>
<Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListView}}" Path="View.Columns[0].Header}}" /> <!-- doesn't contain the word "Name" like i suspected -->
</MultiBinding>
LAST Update
it is a dead end in my case you can't Bind from the GridViewColumn.CellTemplate to the specific Column Header Value
{Binding} implicitely means : {Binding Path=.}.
So you could use
<Binding Path="."/>

Why is ConvertBack not called on this MultiBinding?

My combobox listing Contacts is bound to both FullName and PhoneExtension using MultiBinding. The Convert method of IMultiValueConverter is called but ConvertBack is not. Why? The combobox properly displays the list but the selection is not saved. It disappears when I tab away.
Background:
1) The Contact list comes from a web service and is put in an observable collection ContactListObservable in the code behind. I'm not using a ViewModel.
PhoneBookService phoneBookService = new PhoneBookService();
PhoneRecordArray pbs = GetCompletePhoneListing();
List<PhoneRecord> pbsList = pbs.PhoneArray.ToList();
ObservableCollection<Contact> observableContacts = new ObservableCollection<Contact>();
foreach(PhoneBookService.PhoneRecord rec in pbsList)
{
Contact c = new Contact();
c.FullName = rec.Person;
c.PhoneExtension = rec.Phone;
observableContacts.Add(c);
}
ContactListObservable = observableContacts;
2) The combobox is in a datagrid located on a UserControl. That's the reason for this wacky binding: ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
3) The IMultiValueConverter is a class referenced in UserControl.Resources as <local:CombineNameAndPhoneExtensionMultiConverter x:Key="combinedNameAndPhoneExtensionConverter"/>
4) Legacy data NOT found in the Contacts list must display. This is accomplished with a DataGridTemplateColumn by combining a TextBlock to display values and a ComboBox for editing. See this Julie Lerman MSDN article.
Here is the crazy XAML:
<DataGridTemplateColumn x:Name="DataGridContactTemplateColumn" Header="Contact Using Template">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} Ext. {1}">
<Binding Path="FullName"/>
<Binding Path="PhoneExtension"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="ContactsCellEditingTemplate">
<Grid FocusManager.FocusedElement="{Binding ElementName=ContactsTemplateComboBox}">
<ComboBox x:Name="ContactsTemplateComboBox" IsSynchronizedWithCurrentItem="False" IsEditable="False" IsDropDownOpen="True" ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource combinedNameAndPhoneExtensionConverter}">
<Binding Path="FullName" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="PhoneExtension" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
I've devoted WAY too much time to this so I'll greatly appreciate any help you can offer.
More Background:
The datagrid containing my combobox contains one entity framework contact object per row and includes additional contact fields. Here is some working XAML that succesfully displays and saves FullName but not the phone extension which I want to save in combination with the FullName:
<DataGridTemplateColumn x:Name="DataGridContactTemplateColumn" Header="Contact Using Template">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=FullName}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate x:Name="ContactsCellEditingTemplate">
<Grid FocusManager.FocusedElement="{Binding ElementName=ContactsTemplateComboBox}">
<ComboBox x:Name="ContactsTemplateComboBox" ItemsSource="{Binding ContactListObservable, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" DisplayMemberPath="FullName" SelectedValuePath="FullName" Text="{Binding Path=FullName}" SelectedItem="{Binding Path=FullName}" IsSynchronizedWithCurrentItem="False" IsEditable="False" IsDropDownOpen="True"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
The TextBlock will never change it's Text property, so there is no reason to call the ConvertBack method. You would need to be bind to either the ComboBox's SelectedItem or Text properties to get updates.
I'm answering my own question to elaborate on CodeNaked's accurate answer. Add this to the problem XAML and everything works - ConvertBack is called and both FullName and PhoneExtension are saved as needed:
<ComboBox.SelectedItem>
<MultiBinding Converter="{StaticResource combinedNameAndPhoneExtensionConverter}">
<Binding Path="FullName" UpdateSourceTrigger="PropertyChanged"/>
<Binding Path="PhoneExtension" UpdateSourceTrigger="PropertyChanged"/>
</MultiBinding>
</ComboBox.SelectedItem>
Here is the combinedNameAndPhoneExtensionConverter code:
public class CombineNameAndPhoneExtensionMultiConverter : IMultiValueConverter
{
public object Convert(object[] values,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
if (values[0] as string != null)
{
string fullName = (string)values[0];
string phoneExtension = (string)values[1];
string namePlusExtension = fullName + ", " + phoneExtension;
return namePlusExtension;
}
return null;
}
public object[] ConvertBack(object value,
Type[] targetTypes,
object parameter,
System.Globalization.CultureInfo culture)
{
Contact c = (Contact)value;
string[] returnValues = { c.FullName, c.PhoneExtension };
return returnValues;
}
}
Thanks CodeNaked for your fast reply!

Can I do Text search with multibinding

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.Text doesn't work in a DataTemplate. Otherwise you could have done something like this
<ComboBox ...>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="TextSearch.Text">
<Setter.Value>
<MultiBinding StringFormat="{}{0}: {1}">
<Binding Path="BidServiceCategoryId"/>
<Binding Path="BidServiceCategoryName"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ComboBox.ItemContainerStyle>
</ComboBox>
However this won't work, so I see two solutions to your problem.
First way
You set IsTextSearchEnabled to True for the ComboBox, override ToString in your source class and change the MultiBinding in the TextBlock to a Binding
Xaml
<ComboBox ...
IsTextSearchEnabled="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
Source class
public class TheNameOfYourSourceClass
{
public override string ToString()
{
return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName);
}
//...
}
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 BidServiceCategoryId and BidServiceCategoryName for the TextSearch.TextPath. In this example I call it BidServiceCategory. For this to work, you'll have to call OnPropertyChanged("BidServiceCategory"); when BidServiceCategoryId or BidServiceCategoryName changes as well. If they are normal CLR properties, you can do this in set, and if they are dependency properties you'll have to use the property changed callback
Xaml
<ComboBox ...
TextSearch.TextPath="BidServiceCategory"
IsTextSearchEnabled="True">
<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>
Source class
public class TheNameOfYourSourceClass
{
public string BidServiceCategory
{
get
{
return String.Format("{0}: {1}", BidServiceCategoryId, BidServiceCategoryName);
}
}
private string m_bidServiceCategoryId;
public string BidServiceCategoryId
{
get
{
return m_bidServiceCategoryId;
}
set
{
m_bidServiceCategoryId = value;
OnPropertyChanged("BidServiceCategoryId");
OnPropertyChanged("BidServiceCategory");
}
}
private string m_bidServiceCategoryName;
public string BidServiceCategoryName
{
get
{
return m_bidServiceCategoryName;
}
set
{
m_bidServiceCategoryName = value;
OnPropertyChanged("BidServiceCategoryName");
OnPropertyChanged("BidServiceCategory");
}
}
}
I don't know if your text search has to search ALL the text, but if you want to search from the category ID, you can just set the TextSearch.TextPath property to BidServiceCategoryId. That should also be helpful for anyone who wants to use multibinding and finds that the text search no longer works... It does work if you explicitly set the TextPath property.

Resources