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

前端 未结 1 1702
执念已碎
执念已碎 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

    
        
            
                
            
        
     
    
    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

    
    
    
    public string NameAndId
    {
        return String.Format("{0} | {1}", Name, ID);
    } 
    

    0 讨论(0)
提交回复
热议问题