gpt4 book ai didi

c# - 将 Combobox 绑定(bind)到一个枚举,一个枚举元素除外

转载 作者:行者123 更新时间:2023-11-30 20:20:11 25 4
gpt4 key购买 nike

实际上,我可以使用它们的 DescriptionAttribute 将我的 Combobox 绑定(bind)到 Enum:

<ComboBox ItemsSource="{Binding Source={extension:Enumeration {x:Type model:MyEnum}}}"
DisplayMemberPath="Description"
SelectedValue="{Binding Path=DataContextEnumProp}"
SelectedValuePath="Value"/>

我用过 this SO answer

我的枚举是:

public enum MyEnum
{
[XmlEnum("first")]
[Description("first")]
FirstEnumMember,

[XmlEnum("second")]
[Description("second")]
SecondEnumMember,

//[XmlIgnore]
[Description("second")]
AlternativeToSecondEnumMember //I don't want this one to be in the combobox
}

我需要隐藏 AlternativeToSecondEnumMember,因为它与在 ComboBox 中看到它无关。

为此,最好的解决方案是什么?也许是 Converter 或在代码后面设置 ItemsSource

最佳答案

这是一些使用 ObjectDataProvider 特殊实现的基于属性的解决方案。因此,如果一个枚举字段被 ShouldBeHiddenAttribute 修饰并且其值为 true,则枚举字段将隐藏在 UI(ComboBox) 中。

ShouldBeHidden属性代码

/// <summary>
/// helps to hide the enum value
/// </summary>
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class ShouldBeHiddenAttribute : Attribute
{
public ShouldBeHiddenAttribute(bool isHiddenInUi)
{
HiddenInUi = isHiddenInUi;
}

public bool HiddenInUi { get; set; }
}

用我的属性装饰你的枚举

public enum MyEnum
{
[XmlEnum("first")]
[Description("first")]
FirstEnumMember,

[XmlEnum("second")]
[Description("second")]
SecondEnumMember,

//[XmlIgnore]
[Description("second")]
[ShouldBeHiddenAttribute(true)]
AlternativeToSecondEnumMember
//I don't want this one to be in the combobox
}

特殊的 ObjectDataProvider 实现

/// <summary>
/// helps to display value according to decorating attributes
/// </summary>
public class AttributeBasedObjectDataProvider : ObjectDataProvider
{
/// <summary>
/// returns value of enum according its two attributes
/// 1. DescriptionAttribute - provide a dispaly name of the enum value
/// 2. ShouldBeHiddenAttribute - provide a dispaly state of the enum
/// </summary>
/// <param name="enumObj">enum field value</param>
/// <returns>if ShouldBeHiddenAttribute.HiddenInUi value is true return null else enum Description if defined(or enum actual value id Description is not defined)</returns>
public object GetEnumValues(Enum enumObj)
{
//get the ShouldBeHiddenAttribute value
var isHidden = enumObj.GetType().GetRuntimeField(enumObj.ToString()).
GetCustomAttributes(typeof (ShouldBeHiddenAttribute), false).
SingleOrDefault() as ShouldBeHiddenAttribute;
if (isHidden != null && isHidden.HiddenInUi) return null;
//get the DescriptionAttribute value
var attribute = enumObj.GetType().GetRuntimeField(enumObj.ToString()).
GetCustomAttributes(typeof (DescriptionAttribute), false).
SingleOrDefault() as DescriptionAttribute;
return attribute == null ? enumObj.ToString() : attribute.Description;
}

/// <summary>
/// returns collection of enum values
/// </summary>
/// <param name="type">enum type</param>
/// <returns>collection of enum values</returns>
public List<object> GetShortListOfApplicationGestures(Type type)
{
var shortListOfApplicationGestures =
Enum.GetValues(type).OfType<Enum>().Select(GetEnumValues).Where(o => o != null).ToList();
return
shortListOfApplicationGestures;
}
}

Xaml ObjectDataProvider 定义

    <ObjectDataProvider x:Key="MyEnumMembers" MethodName="GetShortListOfApplicationGestures" ObjectType="{x:Type pageBasedApp:AttributeBasedObjectDataProvider}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="pageBasedApp:MyEnum" />
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>

组合绑定(bind)

<ComboBox ItemsSource="{Binding Source={StaticResource MyEnumMembers}}" Width="150"/>

问候。

关于c# - 将 Combobox 绑定(bind)到一个枚举,一个枚举元素除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37315605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com