Сценарии - Change notify 

Page / - Изменено Alexandr Fokin в 2024/08/25 18:10 

Change notify 
Изменено Alexandr Fokin
в 2024/08/25 18:10

Оглавление

 	C# - Intercepting property changes in subclasses
https://stackoverflow.com/questions/6078859/c-sharp-intercepting-property-changes-in-subclasses
ObservableObject
https://learn.microsoft.com/ru-ru/dotnet/communitytoolkit/mvvm/observableobject	
 	Привязка	
 	/// <summary>
/// Контейнер с INotifyPropertyChanged
/// </summary>
public class ObservableContainer<T>
    : INotifyPropertyChanged,
    IEquatable<ObservableContainer<T>>,
    IEquatable<T>
{
   #region prop

   private T _data;

   public event PropertyChangedEventHandler PropertyChanged;

   public T Data
    {
       get => _data;
       set
        {
            _data = value;
            PropertyChanged?.Invoke(
               this, 
               new PropertyChangedEventArgs(nameof(Data))
                );
        }
    }

   #endregion

   #region ctor

   public ObservableContainer() { }
   public ObservableContainer(T data) 
    {
        _data = data;
    }

   #endregion

   #region override

   public override string ToString()
    {
       return _data?.ToString() ?? "null";
    }

   public override int GetHashCode()
    {
       return _data?.GetHashCode() ?? 0;
    }

   public override bool Equals(object obj)
    {
       if (obj is ObservableContainer<T> o1)
        {
           return Equals(other: o1);
        }
       else if (obj is T o2)
        {
           return Equals(other: o2);
        }

       return false;
    }

   public bool Equals(ObservableContainer<T> other)
    {
       if (other.Data == null && Data == null)
        {
           return true;
        }
       if (other.Data != null)
        { 
            other.Data.Equals(Data);
        }
       if (Data != null)
        {
            Data.Equals(other.Data);
        }

       return false;
    }

   public bool Equals(T other)
    {
       if (other == null && Data == null)
        {
           return true;
        }
       if (other != null)
        {
            other.Equals(Data);
        }
       if (Data != null)
        {
            Data.Equals(other);
        }

       return false;
    }

   #endregion

   #region operator

   public static implicit operator ObservableContainer<T>(T x)
    {
       return new ObservableContainer<T>(x);
    }
   public static implicit operator T(ObservableContainer<T> x)
    {
       return x.Data;
    }

   #endregion
}	
public class TestModel2 
{
   public ObservableContainer<string> Property1 { get; set; } 
        = new ObservableContainer<string>();
   public ObservableContainer<string> Property2 { get; set; } 
        = new ObservableContainer<string>();
}	
<UniformGrid Rows="2" Columns="2">
   <TextBox Text="Prop1.Data"></TextBox>
   <TextBox Text="{Binding Path=Property1.Data, Mode=OneWay}"></TextBox>
   <Button Content="Right Bottom" ></Button>

   <TextBox Text="Prop2.Data"></TextBox>
   <TextBox Text="{Binding Path=Property2.Data, Mode=TwoWay}"></TextBox>
   <Button Content="Right Bottom" Click="Button_Click" ></Button>
</UniformGrid>