C# Winform bind下拉列表(从字典将名称更改为友好名称)
使用.NET4.0,我有一个win-form组合框,可以从字典中绑定,这很好用。但是,我想更改用户在下拉列表中看到的内容的名称 例如。。我希望下拉列表显示10%、20%、30%。。。百分之百 这是我的班级,有百分比和各自的高度值C# Winform bind下拉列表(从字典将名称更改为友好名称),c#,winforms,dictionary,combobox,C#,Winforms,Dictionary,Combobox,使用.NET4.0,我有一个win-form组合框,可以从字典中绑定,这很好用。但是,我想更改用户在下拉列表中看到的内容的名称 例如。。我希望下拉列表显示10%、20%、30%。。。百分之百 这是我的班级,有百分比和各自的高度值 enum SizeType : int { Height_10_Pct = 40, Height_20_Pct = 80, Height_30_Pct = 120, Height_40_Pc
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)
}
创建字典项
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;
}
提前感谢。尝试事件:ComboBox是一个列表控件
尝试以下事件:ComboBox是一个ListControl
在您的特定情况下,您可以简单地使用String.Format{0}%,intval>>2作为字典中的键:
更一般的方法是使用自定义属性。您的代码:
[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;
}
在您的特定情况下,您可以简单地使用String.Format{0}%,intval>>2作为字典中的键:
更一般的方法是使用自定义属性。您的代码:
[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;
}
在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;
}
尽管还有其他答案,但希望它能有所帮助- 在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;
}
尽管还有其他答案,但希望它能有所帮助- 您可以使用switch/case将枚举常量重新绑定到您要查找的新值。您可以使用switch/case将枚举常量重新绑定到您要查找的新值。我最终使用:e.value=e.value.ToString.ReplaceHeight..replacepct,%;最后我使用了:e.Value=e.Value.ToString.ReplaceHeight,.Replace_Pct,%;这太棒了。。。谢谢这太棒了。。。谢谢我最近刚读到了属性。。。很好的解决方案。谢谢,我最近刚读到了属性。。。很好的解决方案。谢谢
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;
}
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;
}