Using VM-M-V pattern with DataGrid?

Oct 20, 2008 at 4:15 PM
Hi,

I have a situation where I have arbitrary sized two dimensional array (i.e int[][]) which I should show to user using datagrid. So far I've managed to create custom template columns and create bindings so that the data is shown properly. So far the problem with this solution seems to be data manipulation which tends to be a bit messy or tricky.

I'd like to solve this using VM-M-V pattern and wrap each data cell into ViewModel object which would be something like this:

class MyViewModel : INotifyPropertyChanged
{
   int value;

   bool isSelected;

   public int Value
   {
      get { return value; }
      set { this.value = value; OnPropertyChanged("Value"); }
   }
  
   public bool IsSelected
   {
      get { return isSelected; }
      set { isSelected = vale; OnPropertyChanged("IsSelected");
   }

   ....
}

Is it possible to bind ViewModel's IsSelected property to DataGridCell's IsSelected property somehow so that I could easily iterate all the selected cells without using DataGrid's API?
Feb 14, 2009 at 12:18 AM
I know this post is a bit old but I did find a way to do this:

<my:DataGrid.ItemContainerStyle>
            <Style TargetType="{x:Type my:DataGridRow}">
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
</my:DataGrid.ItemContainerStyle>

I hope this is helpful to someone.  This is really handy when doing MVVM.