Skip to content

Instantly share code, notes, and snippets.

@maxkatz6
Created October 16, 2020 10:08
Show Gist options
  • Save maxkatz6/159ea4308d57ea897273d06634b7ccbc to your computer and use it in GitHub Desktop.
Save maxkatz6/159ea4308d57ea897273d06634b7ccbc to your computer and use it in GitHub Desktop.
using System;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Data;
using ReactiveUI;
namespace Utils
{
public class ClassSetters : AvaloniaList<ClassSetter>
{
}
public class ClassSetter : AvaloniaObject
{
public static readonly DirectProperty<ClassSetter, string> NameProperty
= AvaloniaProperty.RegisterDirect<ClassSetter, string>(nameof(Name), s => s.Name, (s, v) => s.Name = v, defaultBindingMode: BindingMode.OneTime);
public static readonly StyledProperty<bool> TriggerProperty
= AvaloniaProperty.Register<ClassSetter, bool>(nameof(Trigger), false);
public static readonly AttachedProperty<ClassSetters> ClassesProperty
= AvaloniaProperty.RegisterAttached<ClassSetter, Control, ClassSetters>("Classes");
static ClassSetter()
{
ClassesProperty.Changed.AddClassHandler<Control>(ClassSettersChanged);
}
public static ClassSetters GetClasses(AvaloniaObject obj)
{
return obj.GetValue(ClassesProperty);
}
public static void SetClasses(AvaloniaObject obj, ClassSetters classes)
{
obj.SetValue(ClassesProperty, classes);
}
private static void ClassSettersChanged(Control sender, AvaloniaPropertyChangedEventArgs args)
{
var items = (IAvaloniaReadOnlyList<ClassSetter>)args.NewValue;
if (items != null)
{
foreach (var item in items)
{
_ = item.WhenAnyValue(i => i.Trigger)
.Subscribe(trigger => sender.Classes.Set(item.name, trigger));
}
}
}
private string name;
public string Name
{
get => name;
set => SetAndRaise(NameProperty, ref name, value);
}
public bool Trigger
{
get => GetValue(TriggerProperty);
set => SetValue(TriggerProperty, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment