I knocked my head against the wall for a couple of days, and it was time to ask for help.
I have a DataGrid and a DataForm on the same UserControl. I use the MVVM approach, so there is one ViewModel for UserControl. This ViewModel has a couple of properties that are relevant to this discussion:
public ObservableCollection<VehicleViewModel> Vehicles { get; private set; }
public VehicleViewModel SelectedVehicle
{
get { return selectedVehicle; }
private set
{
selectedVehicle = value;
OnPropertyChanged( "SelectedVehicle" );
}
}
In XAML, my DataGrid and DataForm are defined as follows:
<data:DataGrid SelectionMode="Single"
ItemsSource="{Binding Vehicles}"
SelectedItem="{Binding SelectedVehicle, Mode=TwoWay}"
AutoGenerateColumns="False"
IsReadOnly="True">
<dataFormToolkit:DataForm CurrentItem="{Binding SelectedVehicle}" />
Since the SelectedItem changes in the DataGrid, it must return this change in the ViewModel, and when the ViewModel calls OnPropertyChanged, the DataForm must update itself with the information for the newly selected VehicleViewModel. However, the installer for SelectedVehicle is never called, and in the VS output window I see the following error:
System.Windows.Data: ConvertBack 'xxxx.ViewModel.VehicleViewModel' ( 'xxxx.ViewModel.VehicleViewModel'). BindingExpression: Path = 'SelectedVehicle' DataItem = 'xxxx.ViewModel.MainViewModel' (HashCode = 31664161); "System.Windows.Controls.DataGrid" (Name= ''); target "SelectedItem" ( "System.Object" ). System.MethodAccessException: xxxx.ViewModel.MainViewModel.set_SelectedVehicle (xxxx.ViewModel.VehicleViewModel)
, VehicleViewModel ( ), , ( ). / DataGrid VehicleViewModel ( ItemsSource ObservableCollection ), , SelectedItem , VehicleViewModel.
.