I'm trying to bind a combobox to a dictionary and display a specific field within the currently selected object in WPF.
What I want displayed in the combobox: ("I will do it" is initally selected)
I will do it
I will not do it
I might do it
What is actually displayed currently: (nothing is initally selected)
[YES, AnswerDisplayItem]
[No, AnswerDisplayItem]
[MAYBE, AnswerDisplayItem]
Here's my code:
public enum Answer { YES, NO, MAYBE}
public class AnswerDisplayItem
{
public string DisplayName { get; }
public string DisplayDescription { get; }
public AnswerDisplayItem(string displayName, string displayDescription)
{
DisplayName = displayName;
DisplayDescription = displayDescription;
}
}
public class MyViewModel()
{
public MyViewModel()
{
AnswerDisplay = new Dictionary<Answer, AnswerDisplayItem>
{
{Answer.YES, new AnswerDisplayItem("Yes", "I will do it") },
{Answer.NO, new AnswerDisplayItem("No", "I will not do it")},
{Answer.MAYBE, new AnswerDisplayItem("Maybe", "I might do it")}
};
SelectedAnswer = Answer.Yes;
}
public Dictionary<Answer, AnswerDisplayItem> AnswerDisplay{ get; private set; }
private Answer _selectedAnswer;
public Answer SelectedAnswer
{
get
{
return _selectedAnswer;
}
set
{
if (_selectedAnswer != value)
{
_selectedAnswer = value;
RaisePropertyChanged();
}
}
}
}
XAML:
<ComboBox ItemsSource="{Binding AnswerDisplay}"
DisplayMemberPath="Value.DisplayDescription"
SelectedItem="{Binding SelectedAnswer}"/>
Use a
Dictionary<Answer,string>
(no need for another class)and bind it to the
ComboBox
Update
If you want to use your dictionary, then change the binding to
The desired functionality can be easily achieved by binding to key value property generated from enum:
Define the enum:
The following helper method will return the KeyValue pairs
Property in the ViewModel (or DataContext):
public IEnumerable<KeyValuePair<string, string>> DateModes { get { return EnumHelper.GetAllValuesAndDescriptions<DateModes>(); } }
Binding in the View:
What you want to display is nothing but the description attribute of the enum. I feel this method will be a lot cleaner.