简体   繁体   English

从字典更改名称到友好名称的Winform绑定下拉列表

[英]Winform bind dropdown from dictionary change name to a friendly name

Using .net 4.0 I have a win form combobox where I am binding from a dictionary, this is working fine. 使用.net 4.0我有一个win形式的组合框,我从字典绑定,这工作正常。 However, I would like to change the name of what the user sees in the dropdown. 但是,我想更改用户在下拉列表中看到的名称。

For example.. I would like the dropdown to say 10%, 20%, 30% ... 100%. 例如..我希望下拉列表说10%,20%,30%...... 100%。

Here is my class that has the percentages and respective height values. 这是我的班级,其中包含百分比和各自的高度值。

    enum SizeType : int
    {
        Height_10_Pct = 40,
        Height_20_Pct = 80,
        Height_30_Pct = 120,
        Height_40_Pct = 160,
        Height_50_Pct = 200,
        Height_60_Pct = 240,
        Height_70_Pct = 280,
        Height_80_Pct = 320,
        Height_90_Pct = 360,
        Height_100_Pct = 400)
    }

Creating the dictionary item 创建字典项

    public static Dictionary<string, int> ThumbSizeOptions = new Dictionary<string, int>(BuildThumbSizeOptions());

    public static Dictionary<string, int> BuildThumbSizeOptions()
    {
        ThumbSizeOptions = new Dictionary<string, int>();    
        foreach (SizeType val in Enum.GetValues(typeof(SizeType)))
        {
            ThumbSizeOptions.Add(val.ToString(), (int)((val)));
        }
        return ThumbSizeOptions;
    }

Code to bind in the combo box in the Win Form: 要在Win窗体的组合框中绑定的代码:

     ddlThumbSize.DataSource = new BindingSource(ThumbSizePref.ThumbSizeOptions, null);
     ddlThumbSize.DisplayMember = "Key";
     ddlThumbSize.ValueMember = "Value";

Thank you in advance. 先感谢您。

Try the ListControl.Format event: ( ComboBox is a ListControl ) 尝试ListControl.Format事件:( ComboBox是一个ListControl

ddlThumbSize.Format += (s, e) => {
   e.Value += " %";
};
  1. In your particular case you can simply use String.Format("{0}%", ((int)val) >> 2) as a key in the Dictionary : 在您的特定情况下,您可以简单地使用String.Format("{0}%", ((int)val) >> 2)作为Dictionary的键:

     ThumbSizeOptions.Add(String.Format("{0}%", ((int)val) >> 2), (int)val); 
  2. More general way is to use a custom attribute. 更一般的方法是使用自定义属性。 Code for you: 代码给你:

     [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)] public sealed class EnumDisplayNameAttribute : Attribute { public readonly string Displayname; public EnumDisplayNameAttribute(string displayname) { Displayname = displayname; } public static EnumDisplayNameAttribute Get<T>(T item) { FieldInfo member = typeof(T).GetField(item.ToString()); if (member == null) return null; object[] attrs = member.GetCustomAttributes(typeof(EnumDisplayNameAttribute), true); return attrs.Length == 0 ? null : attrs[0] as EnumDisplayNameAttribute; } } enum SizeType : int { [EnumDisplayName("10%")] Height_10_Pct = 40, [EnumDisplayName("20%")] Height_20_Pct = 80, [EnumDisplayName("30%")] Height_30_Pct = 120, [EnumDisplayName("40%")] Height_40_Pct = 160, [EnumDisplayName("50%")] Height_50_Pct = 200, [EnumDisplayName("60%")] Height_60_Pct = 240, [EnumDisplayName("70%")] Height_70_Pct = 280, [EnumDisplayName("80%")] Height_80_Pct = 320, [EnumDisplayName("90%")] Height_90_Pct = 360, [EnumDisplayName("100%")] Height_100_Pct = 400 } public static Dictionary<string, int> ThumbSizeOptions = new Dictionary<string, int>(BuildThumbSizeOptions()); public static Dictionary<string, int> BuildThumbSizeOptions() { ThumbSizeOptions = new Dictionary<string, int>(); foreach (SizeType val in Enum.GetValues(typeof(SizeType))) { ThumbSizeOptions.Add(EnumDisplayNameAttribute.Get(val).Displayname, (int)val); } return ThumbSizeOptions; } 

In your BuildThumbSizeOptions() you can calculate your comboxbox values like this: BuildThumbSizeOptions()您可以像这样计算您的comboxbox值:

public static Dictionary<string, int> BuildThumbSizeOptions()
{
    ThumbSizeOptions = new Dictionary<string, int>();

    var max = (double)SizeType.Height_100_Pct;
    foreach (int val in Enum.GetValues(typeof(SizeType)))
    {
        double perc = 100.0 / max * (double)val;
        ThumbSizeOptions.Add(perc + "%", val);
    }
    return ThumbSizeOptions;
}

The way you do it now has no dynamic. 你现在的方式没有动态。

Extending this function you can throw away your static enum SizeType and use only one variable which holds the maximum (100%) height. 扩展此函数可以丢弃静态枚举SizeType并仅使用一个保持最大(100%)高度的变量。

public static double MAXIMUM_HEIGHT = 400.0;
public static Dictionary<string, int> BuildThumbSizeOptions()
{
    ThumbSizeOptions = new Dictionary<string, int>();

    for (int perc = 10; perc <= 100; perc += 10)
    {
        var size = MAXIMUM_HEIGHT / 100.0 * perc;
        ThumbSizeOptions.Add(perc + "%", (int)size);
    }
    return ThumbSizeOptions;
}

Hope it helps although there are other answers ;-) 希望它有所帮助,虽然还有其他答案;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM