Add project files.
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF
|
||||
{
|
||||
public abstract class ValueConverter<TSource, TTarget> : MarkupExtension, IValueConverter
|
||||
{
|
||||
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return ConvertTo((TSource)value, targetType, parameter, culture);
|
||||
}
|
||||
|
||||
public object? ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return ConvertBackTo((TTarget)value, targetType, parameter, culture);
|
||||
}
|
||||
|
||||
public TTarget? Convert(TSource value)
|
||||
{
|
||||
return ConvertTo(value, null, null, null);
|
||||
}
|
||||
|
||||
public TSource? ConvertBack(TTarget value)
|
||||
{
|
||||
return ConvertBackTo(value, null, null, null);
|
||||
}
|
||||
|
||||
protected virtual TTarget? ConvertTo(TSource value, Type? targetType, object? parameter, CultureInfo? culture)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
protected virtual TSource? ConvertBackTo(TTarget value, Type? targetType, object? parameter, CultureInfo? culture)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF
|
||||
{
|
||||
public class ValueConverterGroup : List<IValueConverter>, IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return this.Aggregate(value,
|
||||
(current, converter) => converter.Convert(current, targetType, parameter, culture));
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
internal static class ValueBoxes
|
||||
{
|
||||
internal static object CollapsedBox = Visibility.Collapsed;
|
||||
internal static object Double01Box = .1;
|
||||
internal static object Double0Box = .0;
|
||||
internal static object Double100Box = 100.0;
|
||||
internal static object Double10Box = 10.0;
|
||||
internal static object Double1Box = 1.0;
|
||||
internal static object Double200Box = 200.0;
|
||||
internal static object Double20Box = 20.0;
|
||||
internal static object Double300Box = 300.0;
|
||||
internal static object DoubleNeg1Box = -1.0;
|
||||
internal static object FalseBox = false;
|
||||
internal static object HiddenBox = Visibility.Hidden;
|
||||
internal static object HorizontalBox = Orientation.Horizontal;
|
||||
internal static object Int0Box = 0;
|
||||
internal static object Int1Box = 1;
|
||||
internal static object Int2Box = 2;
|
||||
internal static object Int5Box = 5;
|
||||
internal static object Int99Box = 99;
|
||||
internal static object TrueBox = true;
|
||||
internal static object VerticalBox = Orientation.Vertical;
|
||||
internal static object VisibleBox = Visibility.Visible;
|
||||
|
||||
internal static object BooleanBox(bool value) => value ? TrueBox : FalseBox;
|
||||
|
||||
internal static object OrientationBox(Orientation value) =>
|
||||
value == Orientation.Horizontal ? HorizontalBox : VerticalBox;
|
||||
|
||||
internal static object VisibilityBox(Visibility value)
|
||||
{
|
||||
return value switch
|
||||
{
|
||||
Visibility.Visible => VisibleBox,
|
||||
Visibility.Hidden => HiddenBox,
|
||||
Visibility.Collapsed => CollapsedBox,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(value), value, null)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public static class EventArgsExtensions
|
||||
{
|
||||
public static dynamic? GetEventArguments(this EventArgs args, string? path, IValueConverter? converter, object? converterParameter)
|
||||
{
|
||||
if (path is { Length: > 0 })
|
||||
{
|
||||
if (GetEventArgsPropertyPathValue(args, path) is object value)
|
||||
{
|
||||
if (converter is not null)
|
||||
{
|
||||
return converter.Convert(value, typeof(object), converterParameter, CultureInfo.CurrentCulture);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
private static object GetEventArgsPropertyPathValue(object args, string path)
|
||||
{
|
||||
object? value = args;
|
||||
if (path is { })
|
||||
{
|
||||
value = PropertyPathHelper.GetValue(args, path);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public static class FrameworkElementExtensions
|
||||
{
|
||||
|
||||
public static Rect? BoundsRelativeTo(this FrameworkElement source, Visual parent)
|
||||
{
|
||||
try
|
||||
{
|
||||
GeneralTransform generalTransform = source.TransformToAncestor(parent);
|
||||
return generalTransform.TransformBounds(new Rect(0, 0, source.ActualWidth, source.ActualHeight));
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return Rect.Empty;
|
||||
}
|
||||
|
||||
public static bool IsTemplateParent(this FrameworkElement source, FrameworkElement target)
|
||||
{
|
||||
if (source is null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(source, target))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(source?.Parent, target))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
FrameworkElement? parent = source?.Parent as FrameworkElement;
|
||||
while (true)
|
||||
{
|
||||
if (parent is null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(parent, target))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
parent = parent.Parent as FrameworkElement;
|
||||
}
|
||||
|
||||
FrameworkElement? templateParent = source?.TemplatedParent as FrameworkElement;
|
||||
while (true)
|
||||
{
|
||||
if (templateParent is null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (ReferenceEquals(templateParent, target))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
templateParent = templateParent.TemplatedParent as FrameworkElement;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsPointerWithin(this FrameworkElement frameworkElement)
|
||||
{
|
||||
Point position = Mouse.PrimaryDevice.GetPosition(frameworkElement);
|
||||
return position.X >= 0 && position.X <= frameworkElement.ActualWidth && position.Y >= 0 &&
|
||||
position.Y <= frameworkElement.ActualHeight;
|
||||
}
|
||||
|
||||
public static IEnumerable<VisualStateGroup> FindVisualStateGroups(this FrameworkElement frameworkElement)
|
||||
{
|
||||
IEnumerable<VisualStateGroup> visualStateGroups = (IEnumerable<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(frameworkElement);
|
||||
return visualStateGroups ?? Enumerable.Empty<VisualStateGroup>();
|
||||
}
|
||||
|
||||
public static VisualStateGroup? FindVisualGroup(this FrameworkElement frameworkElement, string name)
|
||||
{
|
||||
IEnumerable<VisualStateGroup> visualStateGroups = (IEnumerable<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(frameworkElement);
|
||||
return visualStateGroups?.FirstOrDefault(x => x.Name == name);
|
||||
}
|
||||
|
||||
public static VisualState? FindVisualState(this FrameworkElement frameworkElement, string name)
|
||||
{
|
||||
Collection<VisualStateGroup> visualStateGroups = (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(frameworkElement);
|
||||
return visualStateGroups
|
||||
?.SelectMany(visualStateGroup => visualStateGroup.States.Cast<VisualState>())
|
||||
.FirstOrDefault(visualState => visualState.Name == name);
|
||||
}
|
||||
|
||||
public static VisualTransition? FindVisualTransition(this FrameworkElement frameworkElement, string name)
|
||||
{
|
||||
Collection<VisualStateGroup> visualStateGroups = (Collection<VisualStateGroup>)VisualStateManager.GetVisualStateGroups(frameworkElement);
|
||||
return visualStateGroups
|
||||
?.SelectMany(visualStateGroup => visualStateGroup.Transitions.Cast<VisualTransition>())
|
||||
.FirstOrDefault(visualTransition => visualTransition.To == name);
|
||||
}
|
||||
|
||||
public static bool IsElementFullyVisibleInContainer(this FrameworkElement frameworkElement, UIElement element)
|
||||
{
|
||||
Rect panelRect = element.TransformToAncestor(frameworkElement)
|
||||
.TransformBounds(new Rect(0.0, 0.0, element.DesiredSize.Width, element.DesiredSize.Height));
|
||||
|
||||
double roundedActualHeight = Math.Round(frameworkElement.ActualHeight, 2);
|
||||
double roundedActualWidth = Math.Round(frameworkElement.ActualWidth, 2);
|
||||
|
||||
Rect containerRect = new(0.0, 0.0, roundedActualWidth, roundedActualHeight);
|
||||
|
||||
Point topLeftPointRounded = new(Math.Round(panelRect.TopLeft.X, 2), Math.Round(panelRect.TopLeft.Y, 2));
|
||||
Point topRightPointRounded = new(Math.Round(panelRect.TopRight.X, 2), Math.Round(panelRect.TopRight.Y, 2));
|
||||
Point bottomLeftPointRounded = new(Math.Round(panelRect.BottomLeft.X, 2), Math.Round(panelRect.BottomLeft.Y, 2));
|
||||
Point bottomRightPointRounded = new(Math.Round(panelRect.BottomRight.X, 2), Math.Round(panelRect.BottomRight.Y, 2));
|
||||
|
||||
return containerRect.Contains(topLeftPointRounded) && containerRect.Contains(topRightPointRounded) &&
|
||||
containerRect.Contains(bottomLeftPointRounded) && containerRect.Contains(bottomRightPointRounded);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF
|
||||
{
|
||||
public static class MarkupExtensions
|
||||
{
|
||||
public static Binding ToBinding(this object value)
|
||||
{
|
||||
return value is Binding binding ? binding : new Binding() { Mode = BindingMode.OneWay, Source = value };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class AnimationHelper
|
||||
{
|
||||
public static ThicknessAnimation CreateAnimation(Thickness thickness = default, double milliseconds = 200)
|
||||
{
|
||||
return new(thickness, new Duration(TimeSpan.FromMilliseconds(milliseconds)))
|
||||
{
|
||||
EasingFunction = new PowerEase { EasingMode = EasingMode.EaseInOut }
|
||||
};
|
||||
}
|
||||
public static DoubleAnimation CreateAnimation(double toValue, double milliseconds = 200)
|
||||
{
|
||||
return new(toValue, new Duration(TimeSpan.FromMilliseconds(milliseconds)))
|
||||
{
|
||||
EasingFunction = new PowerEase { EasingMode = EasingMode.EaseInOut }
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF
|
||||
{
|
||||
public static class PropertyPathHelper
|
||||
{
|
||||
private static readonly Dummy dummy = new();
|
||||
|
||||
public static object GetValue(object args, string path)
|
||||
{
|
||||
Binding binding = new(path)
|
||||
{
|
||||
Mode = BindingMode.OneTime,
|
||||
Source = args
|
||||
};
|
||||
|
||||
BindingOperations.SetBinding(dummy, Dummy.ValueProperty, binding);
|
||||
return dummy.GetValue(Dummy.ValueProperty);
|
||||
}
|
||||
|
||||
private class Dummy : DependencyObject
|
||||
{
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register("Value", typeof(object), typeof(Dummy),
|
||||
new UIPropertyMetadata(null));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class BindingProxy : Freezable
|
||||
{
|
||||
public static readonly DependencyProperty DataContextProperty =
|
||||
DependencyProperty.Register(nameof(DataContext),
|
||||
typeof(object), typeof(BindingProxy));
|
||||
|
||||
public object DataContext
|
||||
{
|
||||
get => GetValue(DataContextProperty);
|
||||
set => SetValue(DataContextProperty, value);
|
||||
}
|
||||
|
||||
protected override Freezable CreateInstanceCore()
|
||||
{
|
||||
return new BindingProxy();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.ComponentModel;
|
||||
using System.Globalization;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ChangePropertyExtension : TriggerExtension
|
||||
{
|
||||
private static readonly DependencyProperty PropertyTargetProperty =
|
||||
DependencyProperty.RegisterAttached("PropertyTarget",
|
||||
typeof(object), typeof(ChangePropertyExtension));
|
||||
|
||||
private static readonly DependencyProperty PropertyValueProperty =
|
||||
DependencyProperty.RegisterAttached("PropertyValue",
|
||||
typeof(object), typeof(ChangePropertyExtension));
|
||||
|
||||
private readonly string propertyName;
|
||||
|
||||
private readonly BindingBase targetBinding;
|
||||
|
||||
private readonly BindingBase valueBinding;
|
||||
|
||||
public ChangePropertyExtension(object target, string propertyName, object value)
|
||||
{
|
||||
this.targetBinding = target is BindingBase targetBinding ? targetBinding : target.ToBinding();
|
||||
this.valueBinding = value is BindingBase valueBinding ? valueBinding : value.ToBinding();
|
||||
|
||||
this.propertyName = propertyName;
|
||||
}
|
||||
|
||||
protected override void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
BindingOperations.SetBinding(TargetObject, PropertyTargetProperty, targetBinding);
|
||||
if (TargetObject?.GetValue(PropertyTargetProperty) is { } target)
|
||||
{
|
||||
Type? targetType = target.GetType();
|
||||
if (targetType.GetProperty(propertyName) is PropertyInfo propertyInfo)
|
||||
{
|
||||
BindingOperations.SetBinding(TargetObject, PropertyValueProperty, valueBinding);
|
||||
object? value = TargetObject?.GetValue(PropertyValueProperty);
|
||||
|
||||
TypeConverter? converter = TypeConverterHelper.GetTypeConverter(propertyInfo.PropertyType);
|
||||
object? newValue = value;
|
||||
|
||||
if (value is not null)
|
||||
{
|
||||
if (converter?.CanConvertFrom(value.GetType()) == true)
|
||||
{
|
||||
newValue = converter.ConvertFrom(null, CultureInfo.InvariantCulture, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (converter?.CanConvertTo(propertyInfo.PropertyType) == true)
|
||||
{
|
||||
newValue = converter.ConvertTo(null, CultureInfo.InvariantCulture, value, propertyInfo.PropertyType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (newValue is IEventParameter eventParameter)
|
||||
{
|
||||
newValue = eventParameter.GetParameters(args)[0];
|
||||
}
|
||||
|
||||
propertyInfo?.SetValue(target, newValue, Array.Empty<object>());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
[ContentProperty(nameof(Triggers))]
|
||||
public class CompositeExtension : TriggerExtension
|
||||
{
|
||||
[ConstructorArgument(nameof(Triggers))]
|
||||
public TriggerCollection Triggers { get; } = new TriggerCollection();
|
||||
|
||||
public CompositeExtension(object args0)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0, object args1, object args2, object args3, object args4,
|
||||
object args5, object args6, object args7, object args8, object args9, object args10)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0, object args1, object args2, object args3, object args4,
|
||||
object args5, object args6, object args7, object args8, object args9, object args10, object args11)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
Triggers.Add(args11);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
Triggers.Add(args11);
|
||||
Triggers.Add(args12);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
Triggers.Add(args11);
|
||||
Triggers.Add(args12);
|
||||
Triggers.Add(args13);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
Triggers.Add(args11);
|
||||
Triggers.Add(args12);
|
||||
Triggers.Add(args13);
|
||||
Triggers.Add(args14);
|
||||
}
|
||||
|
||||
public CompositeExtension(object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14,
|
||||
object args15)
|
||||
{
|
||||
Triggers.Add(args0);
|
||||
Triggers.Add(args1);
|
||||
Triggers.Add(args2);
|
||||
Triggers.Add(args3);
|
||||
Triggers.Add(args4);
|
||||
Triggers.Add(args5);
|
||||
Triggers.Add(args6);
|
||||
Triggers.Add(args7);
|
||||
Triggers.Add(args8);
|
||||
Triggers.Add(args9);
|
||||
Triggers.Add(args10);
|
||||
Triggers.Add(args11);
|
||||
Triggers.Add(args12);
|
||||
Triggers.Add(args13);
|
||||
Triggers.Add(args14);
|
||||
Triggers.Add(args15);
|
||||
}
|
||||
|
||||
protected override void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
foreach (TriggerExtension? trigger in Triggers)
|
||||
{
|
||||
trigger.Invoke(sender, args);
|
||||
}
|
||||
|
||||
base.OnInvoked(sender, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ConditionExtension : CompositeExtension
|
||||
{
|
||||
private readonly Binding conditionBinding;
|
||||
|
||||
private static readonly DependencyProperty ConditionProperty =
|
||||
DependencyProperty.RegisterAttached("Condition",
|
||||
typeof(bool), typeof(ConditionExtension));
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0) : base(args0)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1) : base(args0, args1)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2) : base(args0, args1, args2)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3) : base(args0, args1, args2, args3)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4) : base(args0, args1, args2, args3, args4)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5) : base(args0, args1, args2, args3, args4, args5)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6) : base(args0, args1, args2, args3, args4, args5, args6)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7) : base(args0, args1, args2, args3, args4, args5, args6, args7)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10, args11)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10, args11, args12)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10, args11, args12, args13)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10, args11, args12, args13, args14)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
public ConditionExtension(Binding conditionBinding,
|
||||
object args0,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14,
|
||||
object args15) : base(args0, args1, args2, args3, args4, args5, args6, args7, args8, args9, args10, args11, args12, args13, args14, args15)
|
||||
{
|
||||
this.conditionBinding = conditionBinding;
|
||||
}
|
||||
|
||||
protected override void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
BindingOperations.SetBinding(TargetObject, ConditionProperty, conditionBinding);
|
||||
if (TargetObject?.GetValue(ConditionProperty) is true)
|
||||
{
|
||||
base.OnInvoked(sender, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class EventParameterExtension : MarkupExtension, IEventParameter
|
||||
{
|
||||
private readonly IValueConverter? converter;
|
||||
private readonly object? converterParameter;
|
||||
private readonly string? key;
|
||||
private readonly string? path;
|
||||
|
||||
public EventParameterExtension()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public EventParameterExtension(string key, string path)
|
||||
{
|
||||
this.key = key;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public EventParameterExtension(string path)
|
||||
{
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
|
||||
public EventParameterExtension(string path, IValueConverter converter)
|
||||
{
|
||||
this.path = path;
|
||||
this.converter = converter;
|
||||
}
|
||||
|
||||
public EventParameterExtension(string path, IValueConverter converter, object converterParameter)
|
||||
{
|
||||
this.path = path;
|
||||
this.converter = converter;
|
||||
this.converterParameter = converterParameter;
|
||||
}
|
||||
|
||||
public List<object> GetParameters(EventArgs args)
|
||||
{
|
||||
List<object>? parameters = new();
|
||||
|
||||
dynamic? arguments = args.GetEventArguments(path, converter, converterParameter);
|
||||
if (arguments is not null)
|
||||
{
|
||||
if (arguments is ICollection<object> collection)
|
||||
{
|
||||
foreach (object? argument in collection)
|
||||
{
|
||||
parameters.Add(key is not null ? new KeyValuePair<string, object>(key, (dynamic)argument) : argument);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
parameters.Add(key is not null ? new KeyValuePair<string, object>(key, arguments) : arguments);
|
||||
}
|
||||
}
|
||||
|
||||
return parameters;
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,448 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Reflection;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public partial class InvokeExtension : TriggerExtension
|
||||
{
|
||||
private static readonly DependencyProperty ParameterProperty =
|
||||
DependencyProperty.RegisterAttached("Parameter",
|
||||
typeof(object), typeof(InvokeExtension));
|
||||
|
||||
private static readonly DependencyProperty TargetProperty =
|
||||
DependencyProperty.RegisterAttached("Target",
|
||||
typeof(object), typeof(InvokeExtension));
|
||||
|
||||
private readonly Dictionary<Type, Delegate?> dummyHandlers = new();
|
||||
private readonly string name;
|
||||
private readonly List<object> parameters = new();
|
||||
private PropertyChangedRevoker? dataContextPropertyChangedRevoker;
|
||||
|
||||
public InvokeExtension(string name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
parameters.Add(args12 is MarkupExtension ? args12 : args12.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
parameters.Add(args12 is MarkupExtension ? args12 : args12.ToBinding());
|
||||
parameters.Add(args13 is MarkupExtension ? args13 : args13.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
parameters.Add(args12 is MarkupExtension ? args12 : args12.ToBinding());
|
||||
parameters.Add(args13 is MarkupExtension ? args13 : args13.ToBinding());
|
||||
parameters.Add(args14 is MarkupExtension ? args14 : args14.ToBinding());
|
||||
}
|
||||
|
||||
public InvokeExtension(string name,
|
||||
object args1,
|
||||
object args2,
|
||||
object args3,
|
||||
object args4,
|
||||
object args5,
|
||||
object args6,
|
||||
object args7,
|
||||
object args8,
|
||||
object args9,
|
||||
object args10,
|
||||
object args11,
|
||||
object args12,
|
||||
object args13,
|
||||
object args14,
|
||||
object args15)
|
||||
{
|
||||
this.name = name;
|
||||
|
||||
parameters.Add(args1 is MarkupExtension ? args1 : args1.ToBinding());
|
||||
parameters.Add(args2 is MarkupExtension ? args2 : args2.ToBinding());
|
||||
parameters.Add(args3 is MarkupExtension ? args3 : args3.ToBinding());
|
||||
parameters.Add(args4 is MarkupExtension ? args4 : args4.ToBinding());
|
||||
parameters.Add(args5 is MarkupExtension ? args5 : args5.ToBinding());
|
||||
parameters.Add(args6 is MarkupExtension ? args6 : args6.ToBinding());
|
||||
parameters.Add(args7 is MarkupExtension ? args7 : args7.ToBinding());
|
||||
parameters.Add(args8 is MarkupExtension ? args8 : args8.ToBinding());
|
||||
parameters.Add(args9 is MarkupExtension ? args9 : args9.ToBinding());
|
||||
parameters.Add(args10 is MarkupExtension ? args10 : args10.ToBinding());
|
||||
parameters.Add(args11 is MarkupExtension ? args11 : args11.ToBinding());
|
||||
parameters.Add(args12 is MarkupExtension ? args12 : args12.ToBinding());
|
||||
parameters.Add(args13 is MarkupExtension ? args13 : args13.ToBinding());
|
||||
parameters.Add(args14 is MarkupExtension ? args14 : args14.ToBinding());
|
||||
parameters.Add(args15 is MarkupExtension ? args15 : args15.ToBinding());
|
||||
}
|
||||
|
||||
public Binding? BindingTarget { get; set; }
|
||||
|
||||
protected override void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
if (sender is DependencyObject dependencyObject)
|
||||
{
|
||||
if (!TryGetDataContext(dependencyObject, out object dataContext))
|
||||
{
|
||||
dataContextPropertyChangedRevoker = new PropertyChangedRevoker(dependencyObject, FrameworkElement.DataContextProperty, OnDataContextPropertyChangedChanged);
|
||||
void OnDataContextPropertyChangedChanged(object sender, DependencyPropertyChangedEventArgs _)
|
||||
{
|
||||
if (TryGetDataContext(dependencyObject, out dataContext))
|
||||
{
|
||||
dataContextPropertyChangedRevoker?.Dispose();
|
||||
CreaterHandler(dependencyObject, args, dataContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
CreaterHandler(dependencyObject, args, dataContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetInvoke(DependencyObject sender, object dataContext, [AllowNull]out MethodInfo? methodInfo)
|
||||
{
|
||||
methodInfo = null;
|
||||
if (dataContext.GetType().GetMethod(name, BindingFlags.Public | BindingFlags.Instance) is MethodInfo dataContextMethodInfo)
|
||||
{
|
||||
methodInfo = dataContextMethodInfo;
|
||||
return true;
|
||||
}
|
||||
|
||||
if (sender.GetType().GetMethod(name, BindingFlags.Public | BindingFlags.Instance) is MethodInfo senderMethodInfo)
|
||||
{
|
||||
methodInfo = senderMethodInfo;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void CreaterHandler(DependencyObject sender, EventArgs args, object dataContext)
|
||||
{
|
||||
if (TryGetInvoke(sender, dataContext, out MethodInfo? methodInfo))
|
||||
{
|
||||
ParameterInfo[] parameterInfo = methodInfo!.GetParameters();
|
||||
|
||||
List<object> parameters = new();
|
||||
foreach (object? parameter in this.parameters)
|
||||
{
|
||||
switch (parameter)
|
||||
{
|
||||
case IParameter keyedParameter:
|
||||
BindingOperations.SetBinding(sender, ParameterProperty, parameter.ToBinding());
|
||||
parameters.Add(new KeyValuePair<string, object>(keyedParameter.Key, (dynamic)sender.GetValue(ParameterProperty)));
|
||||
break;
|
||||
case IEventParameter eventParameter:
|
||||
parameters.AddRange(eventParameter.GetParameters(args));
|
||||
break;
|
||||
default:
|
||||
BindingOperations.SetBinding(sender, ParameterProperty, parameter.ToBinding());
|
||||
parameters.Add((dynamic)sender.GetValue(ParameterProperty));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (methodInfo is { })
|
||||
{
|
||||
methodInfo.Invoke(dataContext, parameters.Any() ? parameters.ToArray() : parameterInfo.Length > 0 ? new object?[] { null } : Array.Empty<object>());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetDataContext(DependencyObject sender, out object dataContext)
|
||||
{
|
||||
if (BindingTarget is not null)
|
||||
{
|
||||
BindingOperations.SetBinding(sender, TargetProperty, BindingTarget);
|
||||
dataContext = sender.GetValue(TargetProperty);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataContext = sender.GetValue(FrameworkElement.DataContextProperty) ?? sender.GetValue(FrameworkContentElement.DataContextProperty);
|
||||
}
|
||||
|
||||
return dataContext is not null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ParameterExtension : Binding, IParameter
|
||||
{
|
||||
public ParameterExtension(string key)
|
||||
{
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public ParameterExtension(string key, string path) : base(path)
|
||||
{
|
||||
Key = key;
|
||||
}
|
||||
|
||||
public string? Key { get; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class StateExtension : TriggerExtension
|
||||
{
|
||||
private static readonly DependencyProperty PropertyTargetProperty =
|
||||
DependencyProperty.RegisterAttached("PropertyTarget",
|
||||
typeof(object), typeof(StateExtension));
|
||||
|
||||
private static readonly DependencyProperty StateProperty =
|
||||
DependencyProperty.RegisterAttached("State",
|
||||
typeof(object), typeof(StateExtension));
|
||||
|
||||
private readonly BindingBase targetBinding;
|
||||
|
||||
private readonly BindingBase stateBinding;
|
||||
|
||||
public StateExtension(object target, object state)
|
||||
{
|
||||
this.targetBinding = target is BindingBase targetBinding ? targetBinding : target.ToBinding();
|
||||
this.stateBinding = state is BindingBase stateBinding ? stateBinding : state.ToBinding();
|
||||
}
|
||||
|
||||
protected override void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
BindingOperations.SetBinding(TargetObject, PropertyTargetProperty, targetBinding);
|
||||
if (TargetObject?.GetValue(PropertyTargetProperty) is FrameworkElement target)
|
||||
{
|
||||
BindingOperations.SetBinding(target, StateProperty, stateBinding);
|
||||
object? state = target.GetValue(StateProperty);
|
||||
target.ApplyTemplate();
|
||||
|
||||
VisualStateManager.GoToElementState(target, (string)state, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using TheXamlGuy.UI.WPF;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class TriggerCollection : Collection<TriggerExtension>
|
||||
{
|
||||
public void Add(object item)
|
||||
{
|
||||
if (item is TriggerExtension trigger)
|
||||
{
|
||||
base.Add(trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using System.Reflection;
|
||||
using System.Windows.Markup;
|
||||
using System.Windows;
|
||||
using System.Xaml;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
[MarkupExtensionReturnType(typeof(Delegate))]
|
||||
public class TriggerExtension : MarkupExtension
|
||||
{
|
||||
public DependencyObject? TargetObject { get; protected set; }
|
||||
|
||||
protected object? TargetInvoke { get; private set; }
|
||||
|
||||
public void Invoke(object sender, EventArgs args)
|
||||
{
|
||||
OnInvoked(sender, args);
|
||||
}
|
||||
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is IProvideValueTarget target)
|
||||
{
|
||||
if (TargetObject is null)
|
||||
{
|
||||
if (target.TargetObject is DependencyObject dependencyObject)
|
||||
{
|
||||
TargetObject = dependencyObject;
|
||||
}
|
||||
else if (serviceProvider.GetService(typeof(IRootObjectProvider)) is IRootObjectProvider root)
|
||||
{
|
||||
TargetObject = (DependencyObject)root.RootObject;
|
||||
}
|
||||
}
|
||||
|
||||
TargetInvoke = target.TargetProperty;
|
||||
|
||||
MethodInfo invokeMethod = GetType().GetMethod("Invoke", BindingFlags.Instance | BindingFlags.Public)!;
|
||||
if (invokeMethod is null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
switch (TargetInvoke)
|
||||
{
|
||||
case EventInfo info:
|
||||
return Delegate.CreateDelegate(info.EventHandlerType!, this, invokeMethod);
|
||||
case MethodInfo methodInfo:
|
||||
{
|
||||
if (methodInfo.GetParameters() is ParameterInfo[] methodParameters && methodParameters is { Length: 2 })
|
||||
{
|
||||
return Delegate.CreateDelegate(methodParameters[1].ParameterType, this, invokeMethod);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
protected virtual void OnInvoked(object sender, EventArgs args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,35 @@
|
||||
using System.Windows.Media.Effects;
|
||||
using System.Windows;
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class BrightnessEffect : EffectBase
|
||||
{
|
||||
public static readonly DependencyProperty BrightnessProperty =
|
||||
DependencyProperty.Register(nameof(Brightness), typeof(double),
|
||||
typeof(BrightnessEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(0)));
|
||||
|
||||
private static readonly PixelShader Shader;
|
||||
|
||||
static BrightnessEffect()
|
||||
{
|
||||
Shader = new PixelShader
|
||||
{
|
||||
UriSource = new Uri("pack://application:,,,/TheXamlGuy.UI.WPF;component/Resources/Effects/BrightnessEffect.ps")
|
||||
};
|
||||
}
|
||||
|
||||
public BrightnessEffect()
|
||||
{
|
||||
PixelShader = Shader;
|
||||
|
||||
UpdateShaderValue(InputProperty);
|
||||
UpdateShaderValue(BrightnessProperty);
|
||||
}
|
||||
public double Brightness
|
||||
{
|
||||
get => (double)GetValue(BrightnessProperty);
|
||||
set => SetValue(BrightnessProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Windows.Media.Effects;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ColorComplementEffect : EffectBase
|
||||
{
|
||||
private static readonly PixelShader Shader;
|
||||
|
||||
static ColorComplementEffect()
|
||||
{
|
||||
Shader = new PixelShader
|
||||
{
|
||||
UriSource = new Uri("pack://application:,,,/TheXamlGuy.UI.WPF;component/Resources/Effects/ColorComplementEffect.ps")
|
||||
};
|
||||
}
|
||||
|
||||
public ColorComplementEffect()
|
||||
{
|
||||
PixelShader = Shader;
|
||||
|
||||
UpdateShaderValue(InputProperty);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,243 @@
|
||||
using System.Windows.Media.Effects;
|
||||
using System.Windows;
|
||||
using System;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ColorMatrixEffect : EffectBase
|
||||
{
|
||||
public static readonly DependencyProperty M11Property =
|
||||
DependencyProperty.Register("M11",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(0)));
|
||||
|
||||
public static readonly DependencyProperty M12Property =
|
||||
DependencyProperty.Register("M12",
|
||||
typeof(double),typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(5)));
|
||||
|
||||
public static readonly DependencyProperty M13Property =
|
||||
DependencyProperty.Register("M13",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(10)));
|
||||
|
||||
public static readonly DependencyProperty M14Property =
|
||||
DependencyProperty.Register("M14",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(15)));
|
||||
|
||||
public static readonly DependencyProperty M21Property =
|
||||
DependencyProperty.Register("M21",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(1)));
|
||||
|
||||
public static readonly DependencyProperty M22Property =
|
||||
DependencyProperty.Register("M22",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(6)));
|
||||
|
||||
public static readonly DependencyProperty M23Property =
|
||||
DependencyProperty.Register("M23",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(11)));
|
||||
|
||||
public static readonly DependencyProperty M24Property =
|
||||
DependencyProperty.Register("M24",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(16)));
|
||||
|
||||
public static readonly DependencyProperty M31Property =
|
||||
DependencyProperty.Register("M31",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(2)));
|
||||
|
||||
public static readonly DependencyProperty M32Property =
|
||||
DependencyProperty.Register("M32",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(7)));
|
||||
|
||||
public static readonly DependencyProperty M33Property =
|
||||
DependencyProperty.Register("M33",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(12)));
|
||||
|
||||
public static readonly DependencyProperty M34Property =
|
||||
DependencyProperty.Register("M34",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(17)));
|
||||
|
||||
public static readonly DependencyProperty M41Property =
|
||||
DependencyProperty.Register("M41",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(3)));
|
||||
|
||||
public static readonly DependencyProperty M42Property =
|
||||
DependencyProperty.Register("M42",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(8)));
|
||||
|
||||
public static readonly DependencyProperty M43Property =
|
||||
DependencyProperty.Register("M43",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(13)));
|
||||
|
||||
public static readonly DependencyProperty M44Property =
|
||||
DependencyProperty.Register("M44",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(18)));
|
||||
|
||||
public static readonly DependencyProperty M51Property =
|
||||
DependencyProperty.Register("M51",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(4)));
|
||||
|
||||
public static readonly DependencyProperty M52Property =
|
||||
DependencyProperty.Register("M52",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(9)));
|
||||
|
||||
public static readonly DependencyProperty M53Property =
|
||||
DependencyProperty.Register("M53",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(14)));
|
||||
|
||||
public static readonly DependencyProperty M54Property =
|
||||
DependencyProperty.Register("M54",
|
||||
typeof(double), typeof(ColorMatrixEffect), new PropertyMetadata(ValueBoxes.Double0Box, PixelShaderConstantCallback(19)));
|
||||
|
||||
private static readonly PixelShader Shader;
|
||||
|
||||
static ColorMatrixEffect()
|
||||
{
|
||||
Shader = new PixelShader
|
||||
{
|
||||
UriSource = new Uri("pack://application:,,,/TheXamlGuy.UI.WPF;component/Resources/Effects/ColorMatrixEffect.ps")
|
||||
};
|
||||
}
|
||||
|
||||
public ColorMatrixEffect()
|
||||
{
|
||||
PixelShader = Shader;
|
||||
|
||||
UpdateShaderValue(InputProperty);
|
||||
|
||||
UpdateShaderValue(M11Property);
|
||||
UpdateShaderValue(M21Property);
|
||||
UpdateShaderValue(M31Property);
|
||||
UpdateShaderValue(M41Property);
|
||||
UpdateShaderValue(M51Property);
|
||||
|
||||
UpdateShaderValue(M12Property);
|
||||
UpdateShaderValue(M22Property);
|
||||
UpdateShaderValue(M32Property);
|
||||
UpdateShaderValue(M42Property);
|
||||
UpdateShaderValue(M52Property);
|
||||
|
||||
UpdateShaderValue(M13Property);
|
||||
UpdateShaderValue(M23Property);
|
||||
UpdateShaderValue(M33Property);
|
||||
UpdateShaderValue(M43Property);
|
||||
UpdateShaderValue(M53Property);
|
||||
|
||||
UpdateShaderValue(M14Property);
|
||||
UpdateShaderValue(M24Property);
|
||||
UpdateShaderValue(M34Property);
|
||||
UpdateShaderValue(M44Property);
|
||||
UpdateShaderValue(M54Property);
|
||||
}
|
||||
|
||||
public double M11
|
||||
{
|
||||
get => (double)GetValue(M11Property);
|
||||
set => SetValue(M11Property, value);
|
||||
}
|
||||
public double M12
|
||||
{
|
||||
get => (double)GetValue(M12Property);
|
||||
set => SetValue(M12Property, value);
|
||||
}
|
||||
|
||||
public double M13
|
||||
{
|
||||
get => (double)GetValue(M13Property);
|
||||
set => SetValue(M13Property, value);
|
||||
}
|
||||
|
||||
public double M14
|
||||
{
|
||||
get => (double)GetValue(M14Property);
|
||||
set => SetValue(M14Property, value);
|
||||
}
|
||||
|
||||
public double M21
|
||||
{
|
||||
get => (double)GetValue(M21Property);
|
||||
set => SetValue(M21Property, value);
|
||||
}
|
||||
public double M22
|
||||
{
|
||||
get => (double)GetValue(M22Property);
|
||||
set => SetValue(M22Property, value);
|
||||
}
|
||||
|
||||
public double M23
|
||||
{
|
||||
get => (double)GetValue(M23Property);
|
||||
set => SetValue(M23Property, value);
|
||||
}
|
||||
|
||||
public double M24
|
||||
{
|
||||
get => (double)GetValue(M24Property);
|
||||
set => SetValue(M24Property, value);
|
||||
}
|
||||
|
||||
public double M31
|
||||
{
|
||||
get => (double)GetValue(M31Property);
|
||||
set => SetValue(M31Property, value);
|
||||
}
|
||||
public double M32
|
||||
{
|
||||
get => (double)GetValue(M32Property);
|
||||
set => SetValue(M32Property, value);
|
||||
}
|
||||
|
||||
public double M33
|
||||
{
|
||||
get => (double)GetValue(M33Property);
|
||||
set => SetValue(M33Property, value);
|
||||
}
|
||||
|
||||
public double M34
|
||||
{
|
||||
get => (double)GetValue(M34Property);
|
||||
set => SetValue(M34Property, value);
|
||||
}
|
||||
|
||||
public double M41
|
||||
{
|
||||
get => (double)GetValue(M41Property);
|
||||
set => SetValue(M41Property, value);
|
||||
}
|
||||
public double M42
|
||||
{
|
||||
get => (double)GetValue(M42Property);
|
||||
set => SetValue(M42Property, value);
|
||||
}
|
||||
|
||||
public double M43
|
||||
{
|
||||
get => (double)GetValue(M43Property);
|
||||
set => SetValue(M43Property, value);
|
||||
}
|
||||
|
||||
public double M44
|
||||
{
|
||||
get => (double)GetValue(M44Property);
|
||||
set => SetValue(M44Property, value);
|
||||
}
|
||||
|
||||
public double M51
|
||||
{
|
||||
get => (double)GetValue(M51Property);
|
||||
set => SetValue(M51Property, value);
|
||||
}
|
||||
public double M52
|
||||
{
|
||||
get => (double)GetValue(M52Property);
|
||||
set => SetValue(M52Property, value);
|
||||
}
|
||||
public double M53
|
||||
{
|
||||
get => (double)GetValue(M53Property);
|
||||
set => SetValue(M53Property, value);
|
||||
}
|
||||
public double M54
|
||||
{
|
||||
get => (double)GetValue(M54Property);
|
||||
set => SetValue(M54Property, value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Effects;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class ContrastEffect : EffectBase
|
||||
{
|
||||
public static readonly DependencyProperty ContrastProperty =
|
||||
DependencyProperty.Register("Contrast",
|
||||
typeof(double), typeof(ContrastEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(0)));
|
||||
|
||||
private static readonly PixelShader Shader;
|
||||
|
||||
static ContrastEffect()
|
||||
{
|
||||
Shader = new PixelShader
|
||||
{
|
||||
UriSource = new Uri("pack://application:,,,/TheXamlGuy.UI.WPF;component/Resources/Effects/ContrastEffect.ps")
|
||||
};
|
||||
}
|
||||
|
||||
public ContrastEffect()
|
||||
{
|
||||
PixelShader = Shader;
|
||||
|
||||
UpdateShaderValue(InputProperty);
|
||||
UpdateShaderValue(ContrastProperty);
|
||||
}
|
||||
|
||||
public double Contrast
|
||||
{
|
||||
get => (double) GetValue(ContrastProperty);
|
||||
set => SetValue(ContrastProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Windows.Media.Effects;
|
||||
using System.Windows.Media;
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public abstract class EffectBase : ShaderEffect
|
||||
{
|
||||
public static readonly DependencyProperty InputProperty = RegisterPixelShaderSamplerProperty("Input", typeof(EffectBase), 0);
|
||||
|
||||
public Brush Input
|
||||
{
|
||||
get => (Brush)GetValue(InputProperty);
|
||||
set => SetValue(InputProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media.Effects;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class GrayScaleEffect : EffectBase
|
||||
{
|
||||
public static readonly DependencyProperty ScaleProperty =
|
||||
DependencyProperty.Register("Scale",
|
||||
typeof(double), typeof(GrayScaleEffect), new PropertyMetadata(ValueBoxes.Double1Box, PixelShaderConstantCallback(0)));
|
||||
|
||||
private static readonly PixelShader Shader;
|
||||
|
||||
static GrayScaleEffect()
|
||||
{
|
||||
Shader = new PixelShader
|
||||
{
|
||||
UriSource = new Uri("pack://application:,,,/TheXamlGuy.UI.WPF;component/Resources/Effects/GrayScaleEffect.ps")
|
||||
};
|
||||
}
|
||||
|
||||
public GrayScaleEffect()
|
||||
{
|
||||
PixelShader = Shader;
|
||||
|
||||
UpdateShaderValue(InputProperty);
|
||||
UpdateShaderValue(ScaleProperty);
|
||||
}
|
||||
|
||||
public double Scale
|
||||
{
|
||||
get => (double) GetValue(ScaleProperty);
|
||||
set => SetValue(ScaleProperty, value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Windows;
|
||||
using System.Windows.Markup;
|
||||
|
||||
[assembly: InternalsVisibleTo("TheXamlGuy.UI.WPF.Controls")]
|
||||
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
|
||||
[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "TheXamlGuy.UI.WPF")]
|
||||
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF
|
||||
{
|
||||
public class PropertyChangedRevoker : DependencyObject, IDisposable
|
||||
{
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
DependencyProperty.Register(nameof(Value), typeof(object),
|
||||
typeof(PropertyChangedRevoker), new PropertyMetadata(null, OnValuePropertyChanged));
|
||||
|
||||
private readonly WeakReference weakPropertySource;
|
||||
|
||||
public PropertyChangedRevoker(DependencyObject propertySource, string path,
|
||||
DependencyPropertyChangedEventHandler valueChangedHandler) : this(propertySource, new PropertyPath(path),
|
||||
valueChangedHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public PropertyChangedRevoker(DependencyObject propertySource, DependencyProperty property,
|
||||
DependencyPropertyChangedEventHandler valueChangedHandler) : this(propertySource,
|
||||
new PropertyPath(property), valueChangedHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public PropertyChangedRevoker(DependencyObject propertySource, PropertyPath property,
|
||||
DependencyPropertyChangedEventHandler valueChangedHandler)
|
||||
{
|
||||
weakPropertySource = new WeakReference(propertySource);
|
||||
|
||||
Binding binding = new Binding
|
||||
{
|
||||
Path = property,
|
||||
Mode = BindingMode.OneWay,
|
||||
Source = propertySource
|
||||
};
|
||||
|
||||
BindingOperations.SetBinding(this, ValueProperty, binding);
|
||||
ValueChanged = valueChangedHandler;
|
||||
Property = property;
|
||||
}
|
||||
|
||||
public DependencyObject PropertySource
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
return weakPropertySource.IsAlive ? weakPropertySource.Target as DependencyObject : null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public object Value
|
||||
{
|
||||
get => GetValue(ValueProperty);
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
|
||||
public PropertyPath Property { get; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
BindingOperations.ClearBinding(this, ValueProperty);
|
||||
}
|
||||
|
||||
private event DependencyPropertyChangedEventHandler ValueChanged;
|
||||
|
||||
private static void OnValuePropertyChanged(DependencyObject dependencyObject,
|
||||
DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
PropertyChangedRevoker sender = dependencyObject as PropertyChangedRevoker;
|
||||
sender?.OnValuePropertyChanged(args);
|
||||
}
|
||||
|
||||
private void OnValuePropertyChanged(DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
ValueChanged?.Invoke(this, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
sampler2D implicitInput : register(s0);
|
||||
float brightness : register(C0);
|
||||
|
||||
float4 main(float2 uv : TEXCOORD) : COLOR
|
||||
{
|
||||
float4 color = tex2D(implicitInput, uv);
|
||||
color.rgba *= brightness;
|
||||
|
||||
return color;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
sampler2D implicitInput : register(s0);
|
||||
|
||||
float4 main(float2 uv : TEXCOORD) : COLOR
|
||||
{
|
||||
float4 color = tex2D(implicitInput, uv);
|
||||
float4 complement;
|
||||
|
||||
complement.rgb = color.a - color.rgb;
|
||||
complement.a = color.a;
|
||||
|
||||
return complement;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
sampler2D implicitInput : register(s0);
|
||||
float a : register(c0);
|
||||
float b : register(c1);
|
||||
float c : register(c2);
|
||||
float d : register(c3);
|
||||
float e : register(c4);
|
||||
|
||||
float f : register(c5);
|
||||
float g : register(c6);
|
||||
float h : register(c7);
|
||||
float i : register(c8);
|
||||
float j : register(c9);
|
||||
|
||||
float k : register(c10);
|
||||
float l : register(c11);
|
||||
float m : register(c12);
|
||||
float n : register(c13);
|
||||
float o : register(c14);
|
||||
|
||||
float p : register(c15);
|
||||
float q : register(c16);
|
||||
float r : register(c17);
|
||||
float s : register(c18);
|
||||
float t : register(c19);
|
||||
|
||||
/*
|
||||
|
||||
-- -- -- --
|
||||
| a b c d e | | R |
|
||||
| f g h i j | | G |
|
||||
A = | k l m n o | C = | B | R = AC
|
||||
| p q r s t | | A |
|
||||
-- -- | 1 |
|
||||
-- --
|
||||
|
||||
*/
|
||||
|
||||
float4 main(float2 uv : TEXCOORD) : COLOR
|
||||
{
|
||||
float4 color = tex2D(implicitInput, uv);
|
||||
|
||||
float4x4 matrixA1 =
|
||||
{
|
||||
a, b, c, d,
|
||||
f, g, h, i,
|
||||
k, l, m, n,
|
||||
p, q, r, s
|
||||
};
|
||||
|
||||
return mul(matrixA1, color) + float4( e, j, o, t );
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,12 @@
|
||||
sampler2D implicitInput : register(s0);
|
||||
float contrast : register(C0);
|
||||
|
||||
float4 main(float2 uv : TEXCOORD) : COLOR
|
||||
{
|
||||
float4 color = tex2D(implicitInput, uv);
|
||||
float value = (1 - contrast) / 2;
|
||||
|
||||
color.rgba = color.rgba * contrast + value;
|
||||
|
||||
return color;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,15 @@
|
||||
sampler2D implicitInput : register(s0);
|
||||
float scale : register(c0);
|
||||
|
||||
float4 main(float2 uv : TEXCOORD) : COLOR
|
||||
{
|
||||
float4 color = tex2D(implicitInput, uv);
|
||||
|
||||
float4 complement;
|
||||
float intensity = (color.r + color.g + color.b) / 3;
|
||||
|
||||
complement.rgb = color.rgb * (1 - scale) + intensity * scale;
|
||||
complement.a = color.a;
|
||||
|
||||
return complement;
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,9 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class SetterBaseCollection : ObservableCollection<SetterBase>
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class StateTrigger : StateTriggerBase
|
||||
{
|
||||
public static readonly DependencyProperty IsActiveProperty =
|
||||
DependencyProperty.Register(nameof(IsActive),
|
||||
typeof(bool), typeof(StateTrigger),
|
||||
new PropertyMetadata(false, OnIsActivePropertyChanged));
|
||||
|
||||
public bool IsActive
|
||||
{
|
||||
get => (bool)GetValue(IsActiveProperty);
|
||||
set => SetValue(IsActiveProperty, value);
|
||||
}
|
||||
|
||||
private static void OnIsActivePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
(dependencyObject as StateTrigger)?.SetActive((bool)args.NewValue);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public abstract class StateTriggerBase : DependencyObject
|
||||
{
|
||||
internal bool IsTriggerActive { get; private set; }
|
||||
|
||||
internal VisualStateExtension? Owner { get; set; }
|
||||
|
||||
protected void SetActive(bool isActive)
|
||||
{
|
||||
IsTriggerActive = isActive;
|
||||
Owner?.SetActive(IsTriggerActive);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public static class TemplateGenerator
|
||||
{
|
||||
public static DataTemplate CreateDataTemplate(Func<object> factory)
|
||||
{
|
||||
FrameworkElementFactory frameworkElementFactory = new(typeof(TemplateGeneratorControl));
|
||||
frameworkElementFactory.SetValue(TemplateGeneratorControl.FactoryProperty, factory);
|
||||
|
||||
DataTemplate dataTemplate = new(typeof(DependencyObject))
|
||||
{
|
||||
VisualTree = frameworkElementFactory
|
||||
};
|
||||
|
||||
return dataTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class TemplateGeneratorControl : ContentControl
|
||||
{
|
||||
internal static readonly DependencyProperty FactoryProperty =
|
||||
DependencyProperty.Register("Factory", typeof(Func<object>),
|
||||
typeof(TemplateGeneratorControl), new PropertyMetadata(null, OnFactoryPropertyChanged));
|
||||
|
||||
private static void OnFactoryPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
if (dependencyObject is TemplateGeneratorControl sender && args.NewValue is not null)
|
||||
{
|
||||
Func<object> factory = (Func<object>)args.NewValue;
|
||||
sender.Content = factory();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Media.Animation;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
public class VisualStateExtension : VisualState, ISupportInitialize
|
||||
{
|
||||
public static readonly DependencyProperty IsStateTriggersAttachedProperty =
|
||||
DependencyProperty.RegisterAttached("IsStateTriggersAttached",
|
||||
typeof(bool), typeof(VisualStateExtension),
|
||||
new PropertyMetadata(false, EnableStateTriggersChanged));
|
||||
|
||||
private readonly ObservableCollection<StateTriggerBase> stateTriggers = new();
|
||||
|
||||
private Action? afterInit;
|
||||
private FrameworkElement? element;
|
||||
|
||||
private bool isInitializing;
|
||||
private bool lastState;
|
||||
private bool storyboardStateInvalidated;
|
||||
|
||||
public VisualStateExtension()
|
||||
{
|
||||
Name = Guid.NewGuid().ToString();
|
||||
|
||||
Setters = new SetterBaseCollection();
|
||||
Setters.CollectionChanged += OnSettersCollectionChanged;
|
||||
|
||||
stateTriggers.CollectionChanged += OnTriggersCollectionChanged;
|
||||
}
|
||||
|
||||
public SetterBaseCollection Setters { get; }
|
||||
public IList StateTriggers => stateTriggers;
|
||||
|
||||
internal FrameworkElement? Element
|
||||
{
|
||||
get => element;
|
||||
set
|
||||
{
|
||||
element = value;
|
||||
|
||||
if (element is not null)
|
||||
{
|
||||
if (element.IsLoaded)
|
||||
{
|
||||
UpdateActiveState();
|
||||
}
|
||||
else
|
||||
{
|
||||
element.Loaded += OnElementLoaded;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static bool GetIsStateTriggersAttached(FrameworkElement element)
|
||||
{
|
||||
return (bool)element.GetValue(IsStateTriggersAttachedProperty);
|
||||
}
|
||||
|
||||
public static void SetIsStateTriggersAttached(FrameworkElement element, bool value)
|
||||
{
|
||||
element.SetValue(IsStateTriggersAttachedProperty, value);
|
||||
}
|
||||
|
||||
void ISupportInitialize.BeginInit()
|
||||
{
|
||||
isInitializing = true;
|
||||
}
|
||||
|
||||
void ISupportInitialize.EndInit()
|
||||
{
|
||||
isInitializing = false;
|
||||
afterInit?.Invoke();
|
||||
}
|
||||
internal void SetActive(bool active)
|
||||
{
|
||||
if (Element is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (isInitializing)
|
||||
{
|
||||
afterInit = () => SetActive(active);
|
||||
return;
|
||||
}
|
||||
|
||||
if (Storyboard is not null && lastState != active)
|
||||
{
|
||||
if (active)
|
||||
{
|
||||
ClockState state = storyboardStateInvalidated ? Storyboard.GetCurrentState(Element) : ClockState.Stopped;
|
||||
|
||||
if (state == ClockState.Stopped)
|
||||
{
|
||||
Storyboard.Begin(Element, true);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (storyboardStateInvalidated)
|
||||
{
|
||||
Storyboard.Stop(Element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (active)
|
||||
{
|
||||
foreach (Setter setter in Setters.OfType<Setter>())
|
||||
{
|
||||
DependencyProperty property = setter.Property;
|
||||
object value = setter.Value;
|
||||
string targetName = setter.TargetName;
|
||||
DependencyObject? target = Element.FindName(targetName) as DependencyObject;
|
||||
|
||||
if (setter.Value is BindingBase binding)
|
||||
{
|
||||
BindingOperations.SetBinding(target, property, value as BindingBase);
|
||||
}
|
||||
else
|
||||
{
|
||||
target?.SetValue(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastState = active;
|
||||
}
|
||||
|
||||
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
base.OnPropertyChanged(args);
|
||||
if (args.Property.Name == nameof(Storyboard))
|
||||
{
|
||||
if (args.OldValue is Storyboard oldStoryBoard)
|
||||
{
|
||||
oldStoryBoard.CurrentStateInvalidated -= OnStoryboardCurrentStateInvalidated;
|
||||
}
|
||||
|
||||
if (args.NewValue is Storyboard newStoryBoard)
|
||||
{
|
||||
newStoryBoard.CurrentStateInvalidated += OnStoryboardCurrentStateInvalidated;
|
||||
}
|
||||
|
||||
storyboardStateInvalidated = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static void EnableStateTriggersChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
|
||||
{
|
||||
if (dependencyObject is FrameworkElement element)
|
||||
{
|
||||
if ((bool)args.NewValue)
|
||||
{
|
||||
VisualStateManagerContext.Set(element);
|
||||
}
|
||||
else
|
||||
{
|
||||
VisualStateManagerContext.UnSet(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnElementLoaded(object sender, RoutedEventArgs args)
|
||||
{
|
||||
((FrameworkElement)sender).Loaded -= OnElementLoaded;
|
||||
UpdateActiveState();
|
||||
}
|
||||
|
||||
private void OnSettersCollectionChanged(object? sender, EventArgs args)
|
||||
{
|
||||
UpdateActiveState();
|
||||
}
|
||||
private void OnStoryboardCurrentStateInvalidated(object? sender, EventArgs args)
|
||||
{
|
||||
storyboardStateInvalidated = true;
|
||||
}
|
||||
|
||||
private void OnTriggersCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
if (args.NewItems is not null)
|
||||
{
|
||||
foreach (StateTriggerBase item in args.NewItems.OfType<StateTriggerBase>())
|
||||
{
|
||||
item.Owner = this;
|
||||
}
|
||||
}
|
||||
|
||||
if (args.OldItems is not null)
|
||||
{
|
||||
foreach (StateTriggerBase? item in args.OldItems.OfType<StateTriggerBase>().Where(item => item.Owner == this))
|
||||
{
|
||||
item.Owner = null;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateActiveState();
|
||||
}
|
||||
|
||||
private void UpdateActiveState()
|
||||
{
|
||||
SetActive(stateTriggers.Any(t => t.IsTriggerActive));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows;
|
||||
|
||||
namespace TheXamlGuy.UI.WPF;
|
||||
|
||||
internal class VisualStateManagerContext : IDisposable
|
||||
{
|
||||
private static readonly DependencyProperty VisualStateManagerContextProperty =
|
||||
DependencyProperty.RegisterAttached("VisualStateManagerContext",
|
||||
typeof(VisualStateManagerContext), typeof(VisualStateManagerContext));
|
||||
|
||||
private VisualStateManagerContext(FrameworkElement element)
|
||||
{
|
||||
Element = element;
|
||||
|
||||
VisualStateGroups = (ObservableCollection<VisualStateGroup>) element.GetValue(VisualStateManager.VisualStateGroupsProperty);
|
||||
|
||||
VisualStateGroups.CollectionChanged += CollectionChanged;
|
||||
ApplyElement(VisualStateGroups);
|
||||
}
|
||||
|
||||
private FrameworkElement? Element { get; set; }
|
||||
|
||||
private ObservableCollection<VisualStateGroup>? VisualStateGroups { get; set; }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
RemoveElement(VisualStateGroups);
|
||||
if (VisualStateGroups is not null)
|
||||
{
|
||||
VisualStateGroups.CollectionChanged -= CollectionChanged;
|
||||
}
|
||||
|
||||
Element = null;
|
||||
VisualStateGroups = null;
|
||||
}
|
||||
|
||||
internal static void Set(FrameworkElement element)
|
||||
{
|
||||
VisualStateManagerContext hook = new(element);
|
||||
element.SetValue(VisualStateManagerContextProperty, hook);
|
||||
}
|
||||
|
||||
internal static void UnSet(FrameworkElement element)
|
||||
{
|
||||
if (element.GetValue(VisualStateManagerContextProperty) is VisualStateManagerContext hook)
|
||||
{
|
||||
element.SetValue(VisualStateManagerContextProperty, null);
|
||||
hook.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
private void CollectionChanged(object? sender, NotifyCollectionChangedEventArgs args)
|
||||
{
|
||||
if (args.NewItems != null)
|
||||
{
|
||||
ApplyElement(args.NewItems.OfType<VisualStateGroup>());
|
||||
}
|
||||
|
||||
if (args.OldItems != null)
|
||||
{
|
||||
RemoveElement(args.OldItems.OfType<VisualStateGroup>());
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyElement(IEnumerable<VisualStateGroup> visualStateGroups)
|
||||
{
|
||||
foreach (VisualStateExtension? state in visualStateGroups.SelectMany(group => group.States.OfType<VisualStateExtension>()))
|
||||
{
|
||||
state.Element = Element;
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveElement(IEnumerable<VisualStateGroup>? visualStateGroups)
|
||||
{
|
||||
if (visualStateGroups is not null)
|
||||
{
|
||||
foreach (VisualStateExtension? state in visualStateGroups.SelectMany(group => group.States.OfType<VisualStateExtension>()))
|
||||
{
|
||||
state.Element = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0-windows10.0.18362.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UseWPF>true</UseWPF>
|
||||
<RootNamespace>TheXamlGuy.UI.WPF</RootNamespace>
|
||||
<AssemblyName>TheXamlGuy.UI.WPF</AssemblyName>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<None Remove="Resources\Effects\BrightnessEffect.fx" />
|
||||
<None Remove="Resources\Effects\BrightnessEffect.ps" />
|
||||
<None Remove="Resources\Effects\ColorComplementEffect.fx" />
|
||||
<None Remove="Resources\Effects\ColorComplementEffect.ps" />
|
||||
<None Remove="Resources\Effects\ColorMatrixEffect.fx" />
|
||||
<None Remove="Resources\Effects\ColorMatrixEffect.ps" />
|
||||
<None Remove="Resources\Effects\ContrastEffect.fx" />
|
||||
<None Remove="Resources\Effects\ContrastEffect.ps" />
|
||||
<None Remove="Resources\Effects\GrayScaleEffect.fx" />
|
||||
<None Remove="Resources\Effects\GrayScaleEffect.ps" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UI\UI.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Resource Include="Resources\Effects\BrightnessEffect.fx" />
|
||||
<Resource Include="Resources\Effects\BrightnessEffect.ps" />
|
||||
<Resource Include="Resources\Effects\ColorComplementEffect.fx" />
|
||||
<Resource Include="Resources\Effects\ColorComplementEffect.ps" />
|
||||
<Resource Include="Resources\Effects\ColorMatrixEffect.fx" />
|
||||
<Resource Include="Resources\Effects\ColorMatrixEffect.ps" />
|
||||
<Resource Include="Resources\Effects\ContrastEffect.fx" />
|
||||
<Resource Include="Resources\Effects\ContrastEffect.ps" />
|
||||
<Resource Include="Resources\Effects\GrayScaleEffect.fx" />
|
||||
<Resource Include="Resources\Effects\GrayScaleEffect.ps" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user