Custom tooltip for listbox item in WPF

后端 未结 3 1277
遥遥无期
遥遥无期 2021-01-22 23:31

I\'ve got a ListBox that displays an ObservableCollection of Talent objects. When the user hovers over each item in the ListBox, I\'d like to display in the ToolTip several piec

3条回答
  •  旧巷少年郎
    2021-01-23 00:16

    After a quick test your code seems to work fine.

    My test:

    xaml:

     
    
     
        
                
                    
                        
                            
                                
                                    
                                
                            
                        
                    
                
            
         
    
            
                
            
        
    

    Code:

    public partial class MainWindow : Window
    {
        private ObservableCollection _models = new ObservableCollection();
    
        public MainWindow()
        {
            InitializeComponent();
            Models.Add(new MyModel { tName = "Test", Description = "Hello" });
        }
    
        public ObservableCollection Models
        {
            get { return _models; }
            set { _models = value; }
        }
    }
    
    public  class MyModel : INotifyPropertyChanged
    {
        private string _tName;
        private string _description;
    
        public string tName 
        {
            get { return _tName; }
            set { _tName = value; NotifyPropertyChanged("tName"); } 
        }
    
        public string Description
        {
            get { return _description; }
            set { _description = value; NotifyPropertyChanged("Description"); }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }
    

    result: enter image description here

提交回复
热议问题