DataBinding a dictionary to a combobox in WPF.

Posted by Ashish Ashu on Stack Overflow See other posts from Stack Overflow or by Ashish Ashu
Published on 2010-04-12T11:13:29Z Indexed on 2010/04/12 11:23 UTC
Read the original article Hit count: 1332

Filed under:
|
|

I have a Dictionary which is binded to a combobox. I have used dictionary to provide spaces in enum.

public enum Option {Enter_Value, Select_Value};

Dictionary<Option,string> Options;

    <ComboBox                                        
        x:Name="optionComboBox"
        SelectionChanged="optionComboBox_SelectionChanged"
        SelectedValuePath="Key"
        DisplayMemberPath="Value"
        SelectedItem="{Binding Path = SelectedOption}"
        ItemsSource="{Binding Path = Options}" />

This works fine.

My queries:

1. I am not able to set the initial value to a combo box. In above XAML snippet the line

SelectedItem="{Binding Path = SelectedOption}"

is not working. I have declared SelectOption in my viewmodel. This is of type string and I have intialized this string value in my view model as below:

SelectedOption = Options[Options.Enter_Value].ToString();

2. I want if user selects Options.Enter_Value from the combo box , the combo box becomes editable and user can enter the numeric value in it. If user selects the second option Options.Select_Value then a dialog is poped up which allows user to select the predifined values. And the combo box becomes read only and shows the selected value.

Please Help!!

© Stack Overflow or respective owner

Related posts about wpf

Related posts about wpf-binding