Best way to notify property change when field is depending on another

后端 未结 3 1403
粉色の甜心
粉色の甜心 2021-01-31 06:16

What is the best way in c# to notify property changed on an item\'s field without set but get depends on other fields ?

For example :



        
3条回答
  •  甜味超标
    2021-01-31 07:17

    One way is to just call OnPropertyChanged multiple times:

    public MyClass Item
    {
        get
        {
            return _item;
        }
        protected set
        {
            _item = value;
            OnPropertyChanged("Item");
            OnPropertyChanged("Field");
        }
    }
    

    This isn't very maintainable, however. Another option is to add a setter to your get-only property and set it from the other property:

    public MyClass Item
    {
        get
        {
            return _item;
        }
        protected set
        {
            _item = value;
            OnPropertyChanged("Item");
            Field = _item.Field;
        }
    }
    
    public object Field
    {
        get
        {
            return _field;
        }
        private set
        {
            _field = value;
            OnPropertyChanged("Field");
        }
    }
    

    There is no built-in mechanism for using attributes to indicate this relationship between properties, however it would be possible to create a helper class that could do it for you.

    I've made a really basic example of what that might look like here:

    [AttributeUsage( AttributeTargets.Property )]
    public class DepondsOnAttribute : Attribute
    {
        public DepondsOnAttribute( string name )
        {
            Name = name;
        }
    
        public string Name { get; }
    }
    
    public class PropertyChangedNotifier : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public PropertyChangedNotifier( T owner )
        {
            mOwner = owner;
        }
    
        public void OnPropertyChanged( string propertyName )
        {
            var handler = PropertyChanged;
            if( handler != null ) handler( mOwner, new PropertyChangedEventArgs( propertyName ) );
    
            List dependents;
            if( smPropertyDependencies.TryGetValue( propertyName, out dependents ) )
            {
                foreach( var dependent in dependents ) OnPropertyChanged( dependent );
            }
        }
    
        static PropertyChangedNotifier()
        {
            foreach( var property in typeof( T ).GetProperties() )
            {
                var dependsOn = property.GetCustomAttributes( true )
                                        .OfType()
                                        .Select( attribute => attribute.Name );
    
                foreach( var dependency in dependsOn )
                {
                    List list;
                    if( !smPropertyDependencies.TryGetValue( dependency, out list ) )
                    {
                        list = new List();
                        smPropertyDependencies.Add( dependency, list );
                    }
    
                    if (property.Name == dependency)
                        throw new ApplicationException(String.Format("Property {0} of {1} cannot depends of itself", dependency, typeof(T).ToString()));
    
                    list.Add( property.Name );
                }
            }
        }
    
        private static readonly Dictionary> smPropertyDependencies = new Dictionary>();
    
        private readonly T mOwner;
    }
    

    This isn't terribly robust (for example you could create a circular dependency between properties and the property changed would get stuck in an infinite recursion situation). It can also be made simpler using some .NET 4.5 and C#6 features, but I'll leave all that as an exercise for the reader. It probably also doesn't handle inheritance very well.

    To use this class:

    public class Example : INotifyPropertyChanged
    {
        private MyClass _item;
        private PropertyChangedNotifier _notifier;
    
        public Example()
        {
            _notifier = new PropertyChangedNotifier( this );
        }
    
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { _notifier.PropertyChanged += value; }
            remove { _notifier.PropertyChanged -= value; }
        }
    
        public MyClass Item
        {
            get
            {
                return _item;
            }
            protected set
            {
                _item = value;
                OnPropertyChanged("Item");
            }
        }
    
        [DependsOn( "Item" )]
        public object Field
        {
            get
            {
                return _item.Field;
            }
        }
        protected void OnPropertyChanged(string propertyName)
        {
            _notifier.OnPropertyChanged( propertyName );
        }
    }
    

提交回复
热议问题