Add a Setter ComboBox style.
<Setter Property="SelectedItem"> <Setter.Value> <MultiBinding Mode="OneWay"> <MultiBinding.Converter> <local:HelpConverter/> </MultiBinding.Converter> <Binding RelativeSource="{RelativeSource Self}" Path="ItemsSource"/> <Binding RelativeSource="{RelativeSource Self}" Path="Text" UpdateSourceTrigger="PropertyChanged"/> <Binding RelativeSource="{RelativeSource Self}" Path="DisplayMemberPath"/> </MultiBinding> </Setter.Value> </Setter>
and describe the converter itself, which will also work with the DisplayMemberPath in ComboBox
public class HelpConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (values.Length >= 2 && values[0] is IEnumerable && values[1] is string) { string displayString = null; if (values.Length >= 3 && values[2] is string) displayString = (string)values[2]; IEnumerable collection = (IEnumerable)values[0]; IEnumerator enumerator = collection.GetEnumerator(); string equalsText = (string)values[1]; while (enumerator.MoveNext()) { string elementDisplayString = null; if (string.IsNullOrWhiteSpace(displayString)) elementDisplayString = enumerator.Current?.ToString(); else { if (enumerator.Current != null) { System.Reflection.PropertyInfo p = enumerator.Current.GetType().GetProperty(displayString); if (p != null && p.CanRead) elementDisplayString = p.GetValue(enumerator.Current)?.ToString(); } } if (equalsText.Equals(elementDisplayString, StringComparison.InvariantCultureIgnoreCase)) return enumerator.Current; } } return null; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotSupportedException(); } }
If someone has found a more elegant solution, please reply, thanks.