Hi,
I have this enum type:
public enum SchedActiveEnum
{
[Description("All")]
All,
[Description("Y")]
Y,
[Description("N")]
N
}
What is the better way for bind it in a combobox?
I would like to display the description but get the value.
I would like to display the description but get the value.
Thanks.

VulpesRank #55 · 29.2K reputationPosted Feb 19, 2015, 7:41 AM EST
http://codereview.stackexchange.com/questions/39163/loading-a-combobox-with-an-enum-and-binding-to-it
In your case, this code should work for WPF:
Roberto Salemi154 reputationPosted Feb 20, 2015, 5:47 AM EST
VulpesRank #55 · 29.2K reputationPosted Feb 20, 2015, 6:53 AM EST
though I thought you said you wanted to display the Descriptions in your opening post?
Roberto Salemi154 reputationPosted Feb 22, 2015, 11:13 PM EST
private Dictionary
public Dictionary
{
get
{
if (_schedActive == null)
{
_schedActive = new Dictionary
//Permette di visualizzare la Description dell'Enum
var myList = Enum.GetValues(typeof(Definition.SchedActiveEnum)).Cast
{
value,
(Attribute.GetCustomAttribute(
value.GetType().GetField(value.ToString()),
typeof(DescriptionAttribute)) as DescriptionAttribute).Description
}
);
foreach (var item in myList)
{
_schedActive.Add((Definition.SchedActiveEnum)item.value, item.Description);
}
}
return _schedActive;
}
}
in xaml:
ItemsSource="{Binding SchedActive}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
Width="50">
Thanks.
OT. How can I format my code in this forum?
VulpesRank #55 · 29.2K reputationPosted Feb 23, 2015, 3:21 AM EST
Instead, I either paste in code which has already been formatted directly from Visual Studio or prepare the formatted code initially in Notepad and then paste it in from there.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts