Question

I have a ComboBox and I want to bind the selected item text to a string in the view model.

Now I have:

Xaml:

<ComboBox DataContext="{Binding AllDataTypes}" SelectedItem="{Binding Type}" />

ViewModel:

private String type;

public String Type
{
    get
    {
        return type;
    }
    set
    {
        if (type == value)
        {
            return;
        }

        type = value;
        RaisePropertyChanged(() => Type);
    }
}

When I run the program I'm getting an exception:

BindingExpression path error: 'Type' property not found on 'object' ''ObservableCollection`1' (HashCode=34166919)'. BindingExpression:Path=Type; DataItem='ObservableCollection`1' (HashCode=34166919); target element is 'ComboBox' (Name=''); target property is 'SelectedItem' (type 'Object')

Was it helpful?

Solution

Change DataContext to ItemsSource:

<ComboBox ItemsSource="{Binding AllDataTypes}" SelectedItem="{Binding Type}" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top