功能完善
This commit is contained in:
104
NeuWPF/NeoUI/Markup/EnumSourceExtension.cs
Normal file
104
NeuWPF/NeoUI/Markup/EnumSourceExtension.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Markup;
|
||||
|
||||
namespace NeumUI.Markup
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于在XAML中提供枚举类型的值源扩展。此扩展允许开发者直接将枚举类型作为数据源,适用于如ComboBox等控件的ItemsSource绑定。
|
||||
/// </summary>
|
||||
public class EnumSourceExtension : MarkupExtension
|
||||
{
|
||||
private Type? enumType;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置一个值,指示是否将枚举项绑定到其 DescriptionAttribute 的描述。如果设置为 true,则返回的枚举项将包含描述信息,并且只能通过 SelectedValue 来绑定源属性。
|
||||
/// </summary>
|
||||
/// <remarks>当此属性设置为 true 时,提供给 UI 的数据源将会是带有 Value 和 Description 属性的对象列表。其中 Value 对应于枚举值,而 Description 则是从枚举成员上的 DescriptionAttribute 中获取的描述文本。</remarks>
|
||||
public bool BindToDescription { get; set; } = false;
|
||||
|
||||
/// <summary>
|
||||
/// 获取或设置用作数据源的枚举类型。此属性允许开发者指定一个枚举类型,该类型将作为控件(如ComboBox)的数据源。
|
||||
/// </summary>
|
||||
/// <remarks>当设置了此属性后,对应的控件会使用指定的枚举类型的所有成员作为其数据项。如果需要展示枚举成员上的描述信息,请同时设置 BindToDescription 属性为 true,并通过 SelectedValue 绑定源属性。</remarks>
|
||||
public Type? EnumType
|
||||
{
|
||||
get => enumType;
|
||||
set
|
||||
{
|
||||
if (value == enumType) return;
|
||||
if (null != value)
|
||||
{
|
||||
var type = Nullable.GetUnderlyingType(value) ?? value;
|
||||
if (!type.IsEnum)
|
||||
{
|
||||
throw new ArgumentException("类型必须是枚举类型");
|
||||
}
|
||||
}
|
||||
enumType = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 在ResourceDictionary中声明
|
||||
/// </summary>
|
||||
/// <example><c>local:EnumSourceExtension x:Key="EnumBindingSource" EnumType="{x:AppearanceType local:Sex}"</c></example>
|
||||
/// <example><c>ItemsSource="{local:EnumSource EnumType=local:ExampleEnum}" SelectedItem="{Binding ExampleEnum}"</c></example>
|
||||
public EnumSourceExtension()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// 用Markup语法
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <remarks>若需要绑定Description,则需设置BindToDescription为True,同时只能用SelectedValue来绑定源属性</remarks>
|
||||
/// <example><c>ItemsSource="{Binding Source={local:EnumTypeBindingSource {x:AppearanceType local:ExampleEnum}}}"</c></example>
|
||||
public EnumSourceExtension(Type enumType) { EnumType = enumType; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object? ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
var pvt = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
|
||||
|
||||
if (pvt == null) { return null; }
|
||||
|
||||
//如果为空,则返回运行时绑定的类。
|
||||
if (pvt.TargetObject is not FrameworkElement) { return this; }
|
||||
|
||||
if (enumType != null)
|
||||
{
|
||||
var enumValues = Enum.GetValues(enumType);
|
||||
if (!BindToDescription) return enumValues;
|
||||
var items = new List<object>();
|
||||
foreach (var enumValue in enumValues)
|
||||
{
|
||||
var item = new
|
||||
{
|
||||
Value = enumValue,
|
||||
Description = GetDescription(enumValue)
|
||||
};
|
||||
items.Add(item);
|
||||
}
|
||||
|
||||
if (pvt.TargetObject is Selector selector)
|
||||
{
|
||||
selector.DisplayMemberPath = "Description";
|
||||
selector.SelectedValuePath = "Value";
|
||||
}
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
private static string GetDescription(object enumValue)
|
||||
{
|
||||
var fieldInfo = enumValue.GetType().GetField(enumValue.ToString());
|
||||
var descriptionAttribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
|
||||
|
||||
return descriptionAttribute?.Description ?? enumValue.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
76
NeuWPF/NeoUI/Markup/SymbolIconExtension.cs
Normal file
76
NeuWPF/NeoUI/Markup/SymbolIconExtension.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
|
||||
|
||||
|
||||
using System.Windows.Markup;
|
||||
using NeumUI.Assets;
|
||||
|
||||
namespace NeumUI.Markup;
|
||||
|
||||
/// <summary>
|
||||
/// 用于在XAML中方便地使用Material Design图标字体的标记扩展。通过指定符号名称,可以轻松地将图标添加到UI元素中。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用此扩展时,需要确保已正确引用了Material Design图标字体资源。可以通过设置`FontFamily`属性来应用图标字体。
|
||||
/// </remarks>
|
||||
/// <example>
|
||||
/// <code lang="xml"> <TextBlock Text="{enu:SymbolIcon Symbol=AddBox}" FontFamily="{StaticResource MaterialIconFont}"/>
|
||||
/// </code>
|
||||
/// </example>
|
||||
[ContentProperty(nameof(Symbol))]
|
||||
[MarkupExtensionReturnType(typeof(string))]
|
||||
public class SymbolIconExtension : MarkupExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 用于在 XAML 中扩展 Material 符号图标的使用范围,允许直接通过字符串指定图标名称并显示对应的符号。
|
||||
/// </summary>
|
||||
public SymbolIconExtension()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用于将 Material 符号图标转换为字符串表示形式的标记扩展类。此类主要用于在 XAML 中直接使用 Material 符号。
|
||||
/// </summary>
|
||||
public SymbolIconExtension(MaterialSymbol symbol)
|
||||
{
|
||||
Symbol = symbol; }
|
||||
|
||||
/// <summary>
|
||||
/// SymbolIconExtension 类用于在 XAML 中通过标记扩展来设置 Material 符号图标。该类允许开发者通过字符串参数指定 Material 符号的名称,然后将其转换为对应的枚举值。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 使用时需提供一个有效的 MaterialSymbol 枚举成员名作为构造函数参数。这个扩展可以方便地与 UI 元素结合使用,以动态地设置或绑定图标属性。
|
||||
/// </remarks>
|
||||
public SymbolIconExtension(string symbol)
|
||||
{
|
||||
Symbol = (MaterialSymbol)Enum.Parse(typeof(MaterialSymbol), symbol); }
|
||||
|
||||
/// <inheritdoc />
|
||||
public override object ProvideValue(IServiceProvider serviceProvider)
|
||||
{
|
||||
return ((char)(int)Symbol).ToString();
|
||||
|
||||
//SymbolIcon symbolIcon = new(Symbol);
|
||||
|
||||
//if (FontSize > 0)
|
||||
//{
|
||||
// symbolIcon.FontSize = FontSize;
|
||||
//}
|
||||
|
||||
//return symbolIcon;
|
||||
}
|
||||
|
||||
|
||||
// public double FontSize { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 表示用于图标扩展的符号。此属性允许在XAML中直接指定一个符号来创建相应的图标。
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// 该属性是<see cref="SymbolIconExtension"/>类的核心,通过设置不同的<see cref="MaterialSymbol"/>值,
|
||||
/// 可以生成不同样式的图标。它支持在XAML标记中使用,使得界面设计更加直观和灵活。
|
||||
/// </remarks>
|
||||
[ConstructorArgument("symbol")]
|
||||
public MaterialSymbol Symbol { get; set; }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user