使用具有列表< KeyValuePair< UserEnum,String>>>的Windows Forms组合框作为数据源 - [英] Using Windows Forms Combo Box with List<KeyValuePair<UserEnum,String>> as DataSource - C#

查看:207
本文介绍了使用具有列表< KeyValuePair< UserEnum,String>>>的Windows Forms组合框作为数据源 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Windows窗体GUI,我有一个Combo,我需要显示字符串值的列表作为DisplayMembers,并使用用户定义的枚举值列表作为ValueMember。我目前返回一个列表>从我的数据库访问功能,我想绑定到我的组合框。我试过将列表分配给.DataSource属性,将Key分配给.DataMember,将Value分配给.DisplayMember。这显然不是一个有效的方法,因为它不工作。



有人可以给我另一种方法,是良好的形式,实际工作吗?



感谢

解决方案

我使用自己的类 EnumPair< ; 结合两个扩展方法将组合框绑定到带有枚举类型的属性。



查看这是否可以帮助你,

  


comboBox.BindToEnumValue< MyEnumType>(myBindingSourceInstance,PropertyNameOfBindingSource);假设您的表单上有一个名为comboBox的ComboBox,一个名为MyEnumType的枚举项和一个名为MyEnumType的枚举项。 BindingSource的实例。 PropertyNameOfBindingSource应该是您的BindingSource具有列表的类型的属性的名称,其PropertyType为MyEnumType。
后台工作的实现如下,不需要扩展方法,我只是不喜欢写几乎相同的代码行; - )

  public static class ComboBoxExtensions 
{
public static void BindToEnumValue< TEnum>(this ComboBox cbo,BindingSource bs,string propertyName)
{
cbo。 DataSource = EnumPair< TEnum> .GetValuePairList();
cbo.ValueMember = EnumPair< TEnum> .ValueMember;
cbo.DisplayMember = EnumPair< TEnum>。DisplayMember;
cbo.DataBindings.Add(new Binding(SelectedValue,bs,propertyName));
}

public static void BindClear(this ComboBox cbo)
{
cbo.DataSource = null;
cbo.DataBindings.Clear();
}
}

///< summary>
///表示一个< see cref =EnumPair/>由枚举T的值
///和值的字符串表示组成。
///< / summary>
///< remarks>
///使用这个泛型类,每个< see cref =Enum/>可以是
///通过附加值动态增强,例如空的
///条目,这在beeing中用于
///< see cref =ComboBox /> es。
///< / remarks>
///< typeparam name =T>< see cref =Enum/>代表。< / typeparam>
public partial class EnumPair< T>
{
#region常量

public const string ValueMember =EnumValue;
public const string DisplayMember =EnumStringValue;

#endregion

#region构造函数

///< summary>
///初始化< see cref =EnumPair/>的新实例。类。
///< / summary>
public EnumPair()
{
Type t = typeof(T);
if(!t.IsEnum)
{
throw new ArgumentException(Class EnumPair< T>只能用Enum-Types!
}
}

///< summary>
///初始化< see cref =EnumPair/>的新实例。类。
///< / summary>
///< param name =value>枚举的值。< / param>
///< param name =stringValue> The< see cref =string/>枚举的值。< / param>
public EnumPair(T value,string stringValue)
{
Type t = typeof(T);
if(!t.IsEnum)
{
throw new ArgumentException(Class EnumPair< T>只能用Enum-Types!
}

this.EnumValue = value;
this.EnumStringValue = stringValue;
}

#endregion

#region属性

///< summary>
///获取或设置< see cref =EnumPair/>的值部分。
///< / summary>
public T EnumValue {get;组; }

///< summary>
///获取或设置< see cref =EnumPair/>的字符串值。
///< / summary>
public string EnumStringValue {get;组; }

#endregion

#region方法

///< summary>
///返回< see cref =string/>其表示当前< see cref =EnumPair/> ;.
///< / summary>
public override string ToString()
{
return this.EnumStringValue;
}

///< summary>
///生成< see cref =List< T>/>的值
///的< see cref =Enum/> T.
///< / summary>
public static List< EnumPair< T>> GetValuePairList()
{
List< EnumPair< T> list = new List< EnumPair< T>>();
EnumPair< T> pair = new EnumPair< T>();

foreach(Enum.GetValues(typeof(T))中的var项)
{
pair = new EnumPair< T&
pair.EnumValue =(T)item;
pair.EnumStringValue =((T)item).ToString();
list.Add(pair);
}

返回列表;
}

///< summary>
///从枚举值隐式转换为< see cref =EnumPair<>/>从该枚举。
///< / summary>
///< param name =e>要转换的枚举值。< / param>
///< returns> A< see cref =EnumPair<>/>到枚举值。< / returns>
public static implicit operator EnumPair T(T e)
{
Type t = typeof(EnumPair?)。MakeGenericType(e.GetType());
return new EnumPair< T>((T)e,((T)e).ToString());
}

#endregion
}


I'm currently working on a Windows Forms GUI and I have a Combo that I need to display a list of string values as the DisplayMembers and use a list of user defined enum values as the ValueMember. I'm currently returning a List> from my database access function and I would like to bind this to my Combo box. I've tried assigning the list to the .DataSource property, assigning "Key" to .DataMember and "Value" to .DisplayMember. This is clearly not a valid approach as it is not working.

Can someone please give me another approach that is in good form and actually works?

Thanks

解决方案

I do use my own class EnumPair<> in combination with two extension methods to bind comboboxes to Properties with enum types.

See if this can help you, that you can work directly with the enums.

Use it like this after implementation:

comboBox.BindToEnumValue<MyEnumType>(myBindingSourceInstance, "PropertyNameOfBindingSource");

That assumes you have a ComboBox named "comboBox" on your form, an Enum called "MyEnumType" and an instance of a BindingSource. The PropertyNameOfBindingSource should be the name of the Property of the type that your BindingSource has a list of, that has the PropertyType of MyEnumType. Implementation for the background work is found below, the extension methods are not needed, i just do not like write nearly identical lines of code ;-)

public static class ComboBoxExtensions
{
    public static void BindToEnumValue<TEnum>(this ComboBox cbo, BindingSource bs, string propertyName)
    {
        cbo.DataSource = EnumPair<TEnum>.GetValuePairList();
        cbo.ValueMember = EnumPair<TEnum>.ValueMember;
        cbo.DisplayMember = EnumPair<TEnum>.DisplayMember;
        cbo.DataBindings.Add(new Binding("SelectedValue", bs, propertyName));
    }

    public static void BindClear(this ComboBox cbo)
    {
        cbo.DataSource = null;
        cbo.DataBindings.Clear();
    }
}

/// <summary>
/// Represents a <see cref="EnumPair"/> consisting of an value 
/// of an enum T and a string represantion of the value.
/// </summary>
/// <remarks>
/// With this generic class every <see cref="Enum"/> can be
/// dynamically enhanced by additional values, such as an empty
/// entry, which is usefull in beeing used with 
/// <see cref="ComboBox"/>es.
/// </remarks>
/// <typeparam name="T">The type of the <see cref="Enum"/> to represent.</typeparam>
public partial class EnumPair<T>
{
    #region Constants

    public const string ValueMember = "EnumValue";
    public const string DisplayMember = "EnumStringValue";

    #endregion

    #region Constructor

    /// <summary>
    /// Initializes a new instance of the <see cref="EnumPair"/> class.
    /// </summary>
    public EnumPair()
    {
        Type t = typeof(T);
        if (!t.IsEnum)
        {
            throw new ArgumentException("Class EnumPair<T> can only be instantiated with Enum-Types!");
        }
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="EnumPair"/> class.
    /// </summary>
    /// <param name="value">The value of the enum.</param>
    /// <param name="stringValue">The <see cref="string"/> value of the enum.</param>
    public EnumPair(T value, string stringValue)
    {
        Type t = typeof(T);
        if (!t.IsEnum)
        {
            throw new ArgumentException("Class EnumPair<T> can only be instantiated with Enum-Types!");
        }

        this.EnumValue = value;
        this.EnumStringValue = stringValue;
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the value part of the <see cref="EnumPair"/>.
    /// </summary>
    public T EnumValue { get; set; }

    /// <summary>
    /// Gets or sets the string value of the <see cref="EnumPair"/>.
    /// </summary>
    public string EnumStringValue { get; set; }

    #endregion

    #region Methods

    /// <summary>
    /// Returns a <see cref="string"/> that represents the current <see cref="EnumPair"/>.
    /// </summary>
    public override string ToString()
    {
        return this.EnumStringValue;
    }

    /// <summary>
    /// Generates a <see cref="List<T>"/> of the values
    /// of the <see cref="Enum"/> T.
    /// </summary>
    public static List<EnumPair<T>> GetValuePairList()
    {
        List<EnumPair<T>> list = new List<EnumPair<T>>();
        EnumPair<T> pair = new EnumPair<T>();

        foreach (var item in Enum.GetValues(typeof(T)))
        {
            pair = new EnumPair<T>();
            pair.EnumValue = (T)item;
            pair.EnumStringValue = ((T)item).ToString();
            list.Add(pair);
        }

        return list;
    }

    /// <summary>
    /// Implicit conversion from enum value to <see cref="EnumPair<>"/> from that enum.
    /// </summary>
    /// <param name="e">The enum value to convert to.</param>
    /// <returns>A <see cref="EnumPair<>"/> to the enum value.</returns>
    public static implicit operator EnumPair<T>(T e)
    {
        Type t = typeof(EnumPair<>).MakeGenericType(e.GetType());
        return new EnumPair<T>((T)e, ((T)e).ToString());
    }

    #endregion
}

这篇关于使用具有列表&lt; KeyValuePair&lt; UserEnum,String&gt;&gt;&gt;的Windows Forms组合框作为数据源 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆