Files
aistudio-wpf-diagram/AIStudio.Wpf.DiagramDesigner.Additionals/Helpers/EnumHelper.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2021-07-23 09:42:22 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
2022-10-28 22:45:39 +08:00
using AIStudio.Wpf.DiagramDesigner;
2021-07-23 09:42:22 +08:00
2023-01-25 15:58:05 +08:00
namespace AIStudio.Wpf.DiagramDesigner.Additionals
2021-07-23 09:42:22 +08:00
{
public class EnumHelper : DependencyObject
{
public static Type GetEnum(DependencyObject obj)
{
return (Type)obj.GetValue(EnumProperty);
}
2022-05-29 16:02:25 +08:00
public static void SetEnum(DependencyObject obj, Type value)
2021-07-23 09:42:22 +08:00
{
obj.SetValue(EnumProperty, value);
}
// Using a DependencyProperty as the backing store for Enum. This enables animation, styling, binding, etc...
public static readonly DependencyProperty EnumProperty =
DependencyProperty.RegisterAttached("Enum", typeof(Type), typeof(EnumHelper), new PropertyMetadata(null, OnEnumChanged));
private static void OnEnumChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (DesignerHelper.IsInDesignMode)
{
return;
}
2021-07-23 09:42:22 +08:00
var control = sender as ItemsControl;
if (control != null)
{
if (e.NewValue != null)
{
var _enum = Enum.GetValues(e.NewValue as Type);
control.ItemsSource = _enum;
control.AlternationCount = _enum.Length;
}
}
}
}
}