Reputation: 1510
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')
Upvotes: 2
Views: 1524
Reputation: 66439
Change DataContext
to ItemsSource
:
<ComboBox ItemsSource="{Binding AllDataTypes}" SelectedItem="{Binding Type}" />
Upvotes: 3