Last active
February 15, 2021 21:17
-
-
Save maxkatz6/2c765560767f20cf0483be8fac29ff22 to your computer and use it in GitHub Desktop.
AddClassBehavior
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AddClassBehavior : AvaloniaObject, IBehavior | |
{ | |
public IAvaloniaObject AssociatedObject { get; private set; } | |
public string Class | |
{ | |
get => GetValue(ClassProperty); | |
set => SetValue(ClassProperty, value); | |
} | |
public static readonly StyledProperty<string> ClassProperty = AvaloniaProperty.Register<AddClassBehavior, string>(nameof(Class), null); | |
public bool IsEnabled | |
{ | |
get => GetValue(IsEnabledProperty); | |
set => SetValue(IsEnabledProperty, value); | |
} | |
public static readonly StyledProperty<bool> IsEnabledProperty = AvaloniaProperty.Register<AddClassBehavior, bool>(nameof(IsEnabled), false); | |
public void Attach(IAvaloniaObject associatedObject) | |
{ | |
if (!(associatedObject is IStyledElement styledElement)) | |
{ | |
throw new ArgumentException($"{nameof(AddClassBehavior)} supports only IStyledElement"); | |
} | |
AssociatedObject = associatedObject; | |
if (Class is string className) | |
{ | |
styledElement.Classes.Set(className, IsEnabled); | |
} | |
} | |
public void Detach() | |
{ | |
IsEnabled = false; | |
AssociatedObject = null; | |
} | |
protected override void OnPropertyChanged<T>(AvaloniaPropertyChangedEventArgs<T> e) | |
{ | |
base.OnPropertyChanged(e); | |
if (!(AssociatedObject is IStyledElement styledElement)) | |
{ | |
return; | |
} | |
if (e.Property == ClassProperty) | |
{ | |
if (e.OldValue.GetValueOrDefault<string>() is string oldClassName) | |
{ | |
styledElement.Classes.Set(oldClassName, false); | |
} | |
if (e.NewValue.GetValueOrDefault<string>() is string newClassName) | |
{ | |
styledElement.Classes.Set(newClassName, IsEnabled); | |
} | |
} | |
else if (e.Property == IsEnabledProperty) | |
{ | |
if (Class is string className) | |
{ | |
styledElement.Classes.Set(className, IsEnabled); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to work with Avalonia 0.10