C#使用多个不同的枚举填充列表 [英] C# Populate list with several different enums

查看:109
本文介绍了C#使用多个不同的枚举填充列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public   enum 颜色
{
红色,
蓝色,
绿色
}

public enum 品尝
{
酸,
盐,
蓝色
}

等。





我在弄清楚如何将这两个枚举同时绑定到一个列表时遇到了麻烦。它会通过使用数组来解决吗?通常,.DataSource = Enum.GetValues(typeof(Color))当然就足够了。有没有办法在该语法中包含第二个?

解决方案

请参阅我对该问题的评论。



对于其他想法,我可以建议两个来源。首先,看一下我过去收集到的答案: combobox.selectedvalue在winform中显示{} / a> [ ^ ]。



这个想法是:你应该创建一个可以同时使用枚举类型和其他数据的数据类,将这种类型的实例放到列表中。我上面提到的答案显示了如何渲染使用这种类型并在列表中呈现一些适当的文本。



您可以在我的相关文章中找到的第二个想法来源:

​​枚举类型不会枚举!解决.NET和语言限制 [ ^ ],

人类可读的枚举元数据 [ ^ ]。



无论如何,我认为您的最佳方法应该从查看枚举类型和一般数据类型开始。一个边缘的想法是将它们合并,如我在上面引用的第一篇文章中所示。



祝你好运,

-SA


为什么不使用 comboBox.Items.Add(myObject)?喜欢这个......



<前lang =cs> public Form1()
{
InitializeComponent();
ComboBox cb = new ComboBox();
Colors [] colors =(Colors [])(Enum.GetValues( typeof (Colors)));
Tastes [] tastes =(Tastes [])(Enum.GetValues( typeof (Tastes)));
foreach (颜色o 颜色)
{
cb。 Items.Add(O);
}
foreach (口味o 品味)
{
cb.Items.Add(o);
}
.Controls.Add(cb);
cb.SelectedIndexChanged + = new EventHandler(cb_SelectedIndexChanged);
}





据说这个我们现在需要写一个方法来确定哪个 Enum 我们的价值来自。 :)



 私人颜色CurrentColor; 
private Tastes CurrentTaste;
私人颜色? GetColour(ComboBox cb)
{
颜色;
if (cb.SelectedItem!= null
{
if (!Enum.TryParse< Colors>(cb.SelectedItem.ToString(), out color))
{
return null ;
}
返回颜色;
}
返回 null ;
}
私人口味? GetTastes(ComboBox cb)
{
口味;
if (cb.SelectedItem!= null
{
if (!Enum.TryParse< Tastes>(cb.SelectedItem.ToString(), out 味道))
{
return null ;
}
返回品味;
}
返回 null ;
}



现在我们所要做的就是从 comboBox.SelectedIndexChanged 因此



  void  cb_SelectedIndexChanged( object  sender,EventArgs e)
{
ComboBox cb =(ComboBox)sender;
颜色? color = this .GetColour(cb);
口味? taste = this .GetTastes(cb);
if (taste.HasValue)
{
this .CurrentTaste =品味。价值;
}
if (colour.HasValue)
{
this .CurrentColor = colour.Value;
}
}


public enum Colour
    {
        red,
        blue,
        green
    }

public enum Taste
    {
        sour,
        salt,
        blue
    }

etc.



I''m having trouble figuring out how to bind these two enums to one list simultaneously. Would it be solved by using an array? Normally, .DataSource = Enum.GetValues(typeof(Colour)) would suffice of course. Is there any way to include a second in that syntax?

解决方案

Please see my comment to the question.

For other ideas, I can suggest two sources. First, look at my past answers collected in one: combobox.selectedvalue shows {} in winform[^].

The idea is: you should create a data class which can use both enumeration types and some other data, to put instances of this type to the list. My answers referenced above show how to render use such type and render some appropriate text in the list.

The second source of ideas you can find in my related articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

Anyway, I think your best approach should start from the reviewing of your enumeration types, and data types in general. One marginal idea is merging them as shown in my first of the articles referenced above.

Good luck,
—SA


Why not just use comboBox.Items.Add(myObject)? Like this...

public Form1()
{
    InitializeComponent();
    ComboBox cb = new ComboBox();
    Colours[] colours = (Colours[])(Enum.GetValues(typeof(Colours)));
    Tastes[] tastes = (Tastes[])(Enum.GetValues(typeof(Tastes)));
    foreach (Colours o in colours)
    {
        cb.Items.Add(o);
    }
    foreach (Tastes o in tastes)
    {
        cb.Items.Add(o);
    }
    this.Controls.Add(cb);
    cb.SelectedIndexChanged += new EventHandler(cb_SelectedIndexChanged);
}



With this being said we now need to write a method to determine which Enum our value comes from. :)

private Colours CurrentColor;
private Tastes CurrentTaste;
private Colours? GetColour(ComboBox cb)
{
    Colours colour;
    if (cb.SelectedItem != null)
    {
            if(!Enum.TryParse<Colours>(cb.SelectedItem.ToString(), out colour))
            {
                return null;
            }
            return colour;
    }
    return null;
}
private Tastes? GetTastes(ComboBox cb)
{
    Tastes taste;
    if (cb.SelectedItem != null)
    {
        if (!Enum.TryParse<Tastes>(cb.SelectedItem.ToString(), out taste))
        {
            return null;
        }
        return taste;
    }
    return null;
}


Now all we have to do is call these methods from comboBox.SelectedIndexChanged as such

void cb_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cb = (ComboBox)sender;
            Colours? colour = this.GetColour(cb);
            Tastes? taste = this.GetTastes(cb);
            if (taste.HasValue)
            {
                this.CurrentTaste = taste.Value;
            }
            if (colour.HasValue)
            {
                this.CurrentColor = colour.Value;
            }
        }


这篇关于C#使用多个不同的枚举填充列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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