1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-28 16:50:11 +00:00
msfs-popout-panel-manager/Shared/ObservableObject.cs

64 lines
2.3 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Reflection;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Shared
{
public class ObservableObject : INotifyPropertyChanged
{
// Implements Fody.Changed
public event PropertyChangedEventHandler PropertyChanged;
2023-07-12 22:41:31 +00:00
protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
bool hasJsonIgnoreAttribute = false;
var type = sender.GetType();
var propertyInfo = type.GetProperty(e.PropertyName);
if (propertyInfo != null)
{
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
return;
hasJsonIgnoreAttribute = Attribute.IsDefined(propertyInfo, typeof(JsonIgnoreAttribute));
}
PropertyChanged?.Invoke(this, new PropertyChangedExtendedEventArgs(e?.PropertyName, hasJsonIgnoreAttribute));
}
protected void InitializeChildPropertyChangeBinding()
{
Type type = this.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
foreach (PropertyInfo propertyInfo in properties)
{
if (Attribute.IsDefined(propertyInfo, typeof(IgnorePropertyChanged)))
continue;
Type childType = propertyInfo.PropertyType;
if (childType.IsSubclassOf(typeof(ObservableObject)))
{
EventInfo eventInfo = childType.GetEvent("PropertyChanged");
MethodInfo methodInfo = type.GetMethod("OnPropertyChanged", BindingFlags.NonPublic | BindingFlags.Instance);
Delegate dg = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
eventInfo.AddEventHandler(propertyInfo.GetValue(this, null), dg);
}
}
2022-07-23 19:23:32 +00:00
}
}
public class PropertyChangedExtendedEventArgs : PropertyChangedEventArgs
{
2023-07-12 22:41:31 +00:00
public virtual bool DisableSave { get; private set; }
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
public PropertyChangedExtendedEventArgs(string propertyName, bool disableSave) : base(propertyName)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
DisableSave = disableSave;
2022-07-23 19:23:32 +00:00
}
}
}