DataBinbding issue - DataTemplate/ViewModel/ICollectionView

Topics: Prism v4 - WPF 4
Jul 20, 2011 at 1:11 PM

Hi!

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.
This is especially obvious when I click one of the column headers and the
view is sorted - the checked rows are unchecked. I'm using WPF4.

What am I doing wrong here?

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


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

Developer
Jul 20, 2011 at 1:23 PM
Edited Jul 20, 2011 at 1:25 PM

Hi,

Based on my understanding of your scenario, your problem might be happening due to the fact that you're setting your Binding for the IsChecked property in your CheckBox as a OneWay binding (which is the default Mode that is set if you don't explicitly specify one). You could try setting the mode of that binding to TwoWay.

If that doesn't solve your problem, you could upload a repro sample so that we help you detect what could be causing that undesired behavior, or you might ask for support in the WPF forums, where you might find a more precise answer, as this is not strictly related to Prism.

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Jul 20, 2011 at 1:27 PM

Hi Guido!

Thanks for your suggestion but I've already tried to set ...,Mode="TwoWay" but, no, it didn't solvew the problem:( I'll follow your suggestion and post in the WPF forum as well.

Cheers!
/Peter