我試圖理清一個Enum的框架,我的最後一位。
我的目標:我想發送任何枚舉類型並將其轉換爲列表並將其綁定到下拉列表。我將使用ObjectDataSource作爲DataSource的給定下拉列表。我想創建一個只有一個參數的複合控件;枚舉類型。該複合控件將整理數據綁定和所有其他位和bops。
現在,我唯一的問題是將泛型方法轉換爲與ObjectDataSource兼容。
這是我需要在我的ObjectDataSource上使用的當前方法的代碼。因此,此方法工作正常,並返回Enum類型WeekDays的項目列表。不過,我需要相同的功能,但我需要用任何類型的枚舉來替換WeekDays。
代碼:
public class DropDownData
{
public EnumDataItemList GetList()
{
EnumDataItemList items = new EnumDataItemList();
foreach (int value in Enum.GetValues(WeekDays))
{
EnumDataItem item = new EnumDataItem();
WeekDays d = (WeekDays)value;
//Set display text
if (!string.IsNullOrEmpty(DataHandlers.GetAttributeValue<DisplayTextAttribute, string>(d)))
{
//Translation logic goes here
item.Text = DataHandlers.GetAttributeValue<DisplayTextAttribute, string>(d);
}
else
{
//Translation logic goes here
item.Text = Enum.GetName(typeof(WeekDays), value);
}
item.Value = value; //Actual value
item.ToolTip = DataHandlers.GetAttributeValue<ToolTipAttribute, string>(d);
item.Description = DataHandlers.GetAttributeValue<Lia.Library.Enums.CustomAttributes.DescriptionAttribute, string>(d);
item.HelpText = DataHandlers.GetAttributeValue<HelpTextAttribute, string>(d);
item.ExcludeOnBinding = DataHandlers.GetAttributeValue<ExcludeOnBinding, bool>(d);
if (!item.ExcludeOnBinding)
{
items.Add(item);
}
}
return items;
}
}
public class EnumDataItemList : List<EnumDataItem>
{
}
據我知道,我不能用一個通用的方法與ObjectDataSource控件,但是泛型類是罰款。我不能讓它與泛型類一起工作,所有幫助都非常感謝。當所有人都在工作時,我很樂意分享完整的解決方案。
我正在使用Framework 2.0。