Using Custom CellStyle to change appearance of column

Oct 22, 2008 at 8:42 PM

Hi, I want to change the appearance of a column based on a property on the column configuration I have. So I added a style in my xaml and set the CellStyle on the grid. I then created my own CustomColumn class and added a property called IsReadOnly and set the property in the event handler for AutoGeneratingColumn. If the column is called UID then I want to change the background/foreground of the column indicating its readonly, but the IsReadOnly property is never accessed on the custom class and I just can't understand why.

Any help most appreciated.

             <Style x:Key="defaultCellStyle" TargetType="{x:Type dg:DataGridCell}">
                    <Trigger Property="IsEditing" Value="True">
                        <Setter Property="BorderBrush" Value="Red" />
                        <Setter Property="BorderThickness" Value="2" />
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},
                        <Setter Property="Background" Value="Tan" />
                        <Setter Property="Foreground" Value="Red" />

   Private Sub AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
        Dim lobjColumn As ListEditColumn = New ListEditColumn
        lobjColumn.CanUserSort = e.Column.CanUserSort
        lobjColumn.Header = e.Column.Header
        lobjColumn.DataFieldBinding = TryCast(e.Column, DataGridBoundColumn).DataFieldBinding
        If lobjColumn.Header.ToString.Contains("UID") Then
            lobjColumn.IsReadOnly = True
            lobjColumn.CanUserSort = False
        End If
        e.Column = lobjColumn
    End Sub

    Public Class ListEditColumn
        Inherits DataGridTextColumn

        Private mblnIsReadOnly As Boolean = False

        Public Property IsReadOnly() As Boolean
                Return mblnIsReadOnly
            End Get
            Set(ByVal value As Boolean)
                mblnIsReadOnly = value
            End Set
        End Property

Oct 23, 2008 at 2:40 PM
In your ListEditColumn class, try making IsReadOnly a DP or implement INotifyPropertyChanged.  Let me know if that helps.
Oct 23, 2008 at 2:45 PM
Thanks it was the SetValue and the DependencyProperty that I missed out of my class. Its working beautifully now... Thanks