Reputation: 1579
I have an application that uses MVVM. I'm trying to set up the databinding for my ComboBox by connecting it to the Properties in my ViewModel. When I run the application I get this error message:
Message='Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '11' and line position '176'.
The problem occurs with this line of XAML:
<ComboBox x:Name="schoolComboBox" HorizontalAlignment="Left" Margin="25,80,0,0" VerticalAlignment="Top" Width="250" FontSize="16" ItemsSource="{Binding LocationList}" SelectedItem="{Binding Source=LocationPicked}" />
Below is the ViewModel that I'm trying to use.
using QMAC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace QMAC.ViewModels
{
class MainViewModel : ViewModelBase
{
Address address;
Location location;
private string _locationPicked;
public MainViewModel()
{
address = new Address();
location = new Location();
}
public List<string> LocationList
{
get { return location.site; }
set
{
OnPropertyChanged("LocationList");
}
}
public string LocationPicked
{
get { return _locationPicked; }
set
{
_locationPicked = value;
MessageBox.Show(_locationPicked);
OnPropertyChanged("LocationPicked");
}
}
}
}
Am I setting up the property incorrectly for it to work with the databinding?
Upvotes: 1
Views: 2635
Reputation: 3111
You are not binding the SelectedItem
correctly. You need to set the Path
on the binding and not the Source
. I'm assuming that you have set the datacontext to the MainViewModel. Since the LocationPicked
property is in the MainViewModel you don't need to set the Binding.Source
. Change your binding to set the Path on the SelectedItem using {Binding LocationPicked
.
Upvotes: 3