Notify Property Changed on a Dictionary

前端 未结 5 1843
余生分开走
余生分开走 2020-12-09 23:11

I have a WPF / XAML form data-bound to a property in a dictionary, similar to this:


<

5条回答
  •  无人及你
    2020-12-09 23:53

    You can just create a class that implement INotifyPropertyChanged interface, wrap your value by this class and use it in Dictionary. I had a similar problem and did next:

    class Parameter : INotifyPropertyChanged //wrapper
    {
        private string _Value;
        public string Value //real value
        {
            get { return _Value; }
            set { _Value = value; RaisePropertyChanged("Value"); } 
        }
    
        public Parameter(string value)
        {
            Value = value;
        }
    
        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    
    public class ViewModel
    {
        public Dictionary Parameters
    }
    
    
        
            
                
                    
                        
                        
                    
                    
            
        
    
    

    After that, if you change values in the dictionary, you will see the changes in UI.

提交回复
热议问题