WPF: Changing a ComboBox's ItemTemplate removes the ability to jump down the list as you type. Any way to fix this?

前端 未结 1 1667
执念已碎
执念已碎 2021-02-04 13:14

PersonVM.cs

public class MainWindowVM
{
    public MainWindowVM()
    {
        PersonList = new ObservableCollection(Employees);
    }

            


        
相关标签:
1条回答
  • 2021-02-04 13:46

    See my answer to this question: Can I do Text search with multibinding

    Unfortunately TextSearch.Text doesn't work in a DataTemplate. I think you have two options here

    Option 1. Set IsTextSearchEnabled to True for the ComboBox, override ToString in your source class and change the MultiBinding in the TextBlock to a Binding

    <ComboBox ...
              IsTextSearchEnabled="True">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox> 
    
    public class Person
    {
        public override string ToString()
        {
            return String.Format("{0} | {1}", Name, ID);
        }
    
        public string Name { get; set; }
        public int ID { get; set; }
    }
    

    Option 2. Make a new Property in your source class where you combine Name and ID for the TextSearch.TextPath. Also, you should call OnPropertyChanged for NameAndId whenever you do it for Name or ID

    <ComboBox ...
              TextSearch.TextPath="NameAndId"
              IsTextSearchEnabled="True">
    
    
    public string NameAndId
    {
        return String.Format("{0} | {1}", Name, ID);
    } 
    
    0 讨论(0)
提交回复
热议问题