views:

200

answers:

2

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. The combobox is binded to datadictionary which have two options first is "Enter_value" and second is "Select_value" which is actually Option enum.

Based on the Option enum value I want to perform different action.

For example

if option is equal to option.Enter_value then

Combo box becomes editable and user can enter the numeric value in it.

if option is equal to option.Select_value value then

the value comes from the database and the combo box becomes read only and shows the fetched value from the database.

Please Help!!

+1  A: 

Your problem, probably, is that you've bound SelectedItem to a property of the wrong type.

An ItemsControl iterates over its ItemsSource's enumerator to build its list of items. The enumerator for your dictionary is of type KeyValuePair<Option, string>. So your SelectedOption property must also be of that type - if you look in the Output window when your application is running, you'll probably see a data-binding error to that effect there.

I can't understand your second question.

Edit

Okay, it's a lot easier to just provide a working example than to explain why code that I can't see isn't working.

First, you need a view model class that implements INotifyPropertyChanged and that exposes SelectedItem, Value, and IsValueReadOnly properties, and that correctly raises PropertyChanged events for those properties when the selected item changes.

public enum Option
{
    EditOption,
    OtherOption
} ;

public class MyViewModel : INotifyPropertyChanged
{
    private Dictionary<Option, string> _Items;
    private KeyValuePair<Option, string> _SelectedItem;
    private string _Value;

    public MyViewModel()
    {
        _Items = new Dictionary<Option, string>
        {
            {Option.EditOption, "Editable value"},
            {Option.OtherOption, "Other value"}
        };
    }

    public Dictionary<Option, string> Items
    {
        get { return _Items; }
    }

    public KeyValuePair<Option, string> SelectedItem
    {
        get { return _SelectedItem; }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged("SelectedItem");
            OnPropertyChanged("IsValueReadOnly");
            OnPropertyChanged("Value");
        }
    }

    public bool IsValueReadOnly
    {
        get { return _SelectedItem.Key != Option.EditOption; }
    }

    public string Value
    {
        get { return IsValueReadOnly ? "Read-only" : _Value; }
        set { _Value = value; }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler h = PropertyChanged;
        if (h != null)
        {
            h(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Now the XAML for your ComboBox and TextBox looks like this:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:WpfApplication6="clr-namespace:WpfApplication6" 
        Title="MainWindow">
    <Window.DataContext>
        <WpfApplication6:MyViewModel/>
    </Window.DataContext>
    <StackPanel>
      <ComboBox ItemsSource="{Binding Items}" 
                DisplayMemberPath="Key"
                SelectedItem="{Binding SelectedItem}"/>
      <TextBox Text="{Binding Value}"
               IsReadOnly="{Binding IsValueReadOnly}"/>
    </StackPanel>
</Window>
Robert Rossney
Thanks Robert for the reply. I will try the first solution, also I have re-framed the second question, please see if you can give me the solution
Ashish Ashu
I have tried the first solution but it doesn't work. Still I get selected index -1
Ashish Ashu
+1  A: 

Try binding SelectedValue, not SelectedItem if SelectedOption is of type Option.

About your second question: Based on selection you can hide your ComboBox and display a TextBlock or TextBox in it's place. Or you can use RadioButtons and enable or disable input accordingly.

majocha
Thanks for the reply majocha.. for the second solution , actually I am using the listview and combo box is displayed in one of the column. I have defined as a GridView. Cell Template. Therefore I can't use radio button or show/Hide the control.
Ashish Ashu