This is a migrated thread and some comments may be shown as answers.

How to get CellStyleSelector to fire on either VM property change, or column property change

1 Answer 30 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 07 Dec 2011, 08:18 PM
Hi, I'm using a MVVM approach to complete the followign simple scenario:

I want all ReadOnly cells to have a certain style. The IsReadOnly property of my GridViewDataColumn is bound to a property in my VM. My ItemsSource is also changed in  my VM, based on this property.

<telerik:GridViewDataColumn IsReadOnly="{Binding IsVMReadOnly}" DataMemberBinding="{Binding Column1}" CellStyleSelector="{StaticResource myCellStyle}"/>

When the property changes, the ItemsSource changes, and new rows get populated. Then CellStyleSelector gets called:
public override Style SelectStyle(object item, DependencyObject container)
{
    Telerik.Windows.Controls.GridView.GridViewCell cell = container as Telerik.Windows.Controls.GridView.GridViewCell;
    if (cell.Column.IsReadOnly)
        return ReadOnlyCellStyle;
    else
        return EditableCellStyle;
}

however, at this point, the column's IsReadOnly property is not updated yet, so the cell's style is wrong.

The easiest solution I can think of is to reapply styles once the binding kicks in. How would I go about doing this?

1 Answer, 1 is accepted

Sort by
0
Aaron
Top achievements
Rank 1
answered on 07 Dec 2011, 08:38 PM
Well, managed to make a really ugly workaround in the CellStyleSelector at least.

Telerik.Windows.Controls.GridView.GridViewCell cell = container as Telerik.Windows.Controls.GridView.GridViewCell;
                if (cell.Column.IsReadOnly || ((MyViewModel)((Telerik.Windows.Controls.GridViewColumn)(((Telerik.Windows.Controls.GridView.GridViewCellBase)(cell)).DataColumn)).DataControl.DataContext).IsVMReadOnly)
                    return ReadOnlyCellStyle;
                else
                    return cell.Style;
Tags
GridView
Asked by
Aaron
Top achievements
Rank 1
Answers by
Aaron
Top achievements
Rank 1
Share this question
or