DataBinding issue - DataTemplate/ViewModel/ICollectionView

Jul 20, 2011 at 1:31 PM


Initially I posted this to the PRISM4 forum but got a suggestion that I should
try this forum as well:) I'm using WPF4 BTW...

I'm running PRISM4 and I've been struggling to get my data binding to work.
I'm following the MVVM pattern and have a view model which initially loads
data from a RDBMS and wraps it in an ICollectionView. After the initial load
the view model listens for events which adds data to the ICollectionView.
This works perfectly but, as I said, I'm struggling when trying to persist
changes made to the data which is presented in a DataGrid declared below.

The view model publishes the ICollectionView through a read/write property,
Results, which, as you can see has a binding mode of "TwoWay". I thought
this would be enough to persist the changes made to the state of the
checkboxes but no:( I've experimented with a number of ways to accomplish
this but the state of the checkbox is not propagated back to the view model.
I've intercepted the call to the "PlotClicked" method which is an ICommand
object but the argument being passed has an unchanged "Plot" attribute!
This is especially obvious when I click one of the column headers and the
view is sorted - the checked rows are unchecked.

What am I doing wrong here?

Many thanks in advance - I'm really stuck here:(

<DataGrid Grid.Row="0" Name="gridResults" ItemsSource="{Binding Results,Mode=TwoWay}" AutoGenerateColumns="False">
  <DataGridTemplateColumn Header="Plot">
     <CheckBox IsChecked="{Binding Path=Plot, Mode=TwoWay}"
         Command="{Binding Path=DataContext.PlotClicked,Mode=OneWay, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"
         CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"/>