CellTemplateSelector for .NET 3.5 DataGrid is passed DataRowView and not cell contents

251 views Asked by At

I am using the DataGrid that comes in the WpfToolkit for .NET 3.5. I am displaying DataTable. I want negative amounts to be red. The number of columns is variable so AutoGenerateColumns is True.

Following the advice here http://wpftutorial.net/DataGrid.html I have done the following. However whenever TemplateSelector is called item is a DataRowView and not cell contents. How can I reference the cell contents??

Public Class AmountDataGrid
Inherits Microsoft.Windows.Controls.DataGrid

Public Property CellTemplateSelector() As DataTemplateSelector
    Get
        Return DirectCast(GetValue(CellTemplateSelectorProperty), DataTemplateSelector)
    End Get
    Set(value As DataTemplateSelector)
        SetValue(CellTemplateSelectorProperty, value)
    End Set
End Property

Public Shared ReadOnly CellTemplateSelectorProperty As DependencyProperty = DependencyProperty.Register("Selector", GetType(DataTemplateSelector), GetType(AmountDataGrid), New FrameworkPropertyMetadata(Nothing))

Protected Overrides Sub OnAutoGeneratingColumn(e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    e.Cancel = True
    Columns.Add(New Microsoft.Windows.Controls.DataGridTemplateColumn() With {.Header = e.Column.Header, .CellTemplateSelector = CellTemplateSelector})
End Sub
End Class

DataGrid in XAML

<l:AmountDataGrid x:Name="dgMonths" Style="{StaticResource MonthsView}" CellTemplateSelector="{StaticResource TemplateSelector}"/>                

Selector

Public Class TemplateSelector
Inherits DataTemplateSelector

Private _defaultTemplate As DataTemplate
Public Property DefaultTemplate() As DataTemplate
    Get
        Return _defaultTemplate
    End Get
    Set(value As DataTemplate)
        _defaultTemplate = value
    End Set
End Property

Public Overrides Function SelectTemplate(item As Object, container As System.Windows.DependencyObject) As DataTemplate

    Return Me.DefaultTemplate

End Function
End Class

Code that binds the DataTable

    strSelect = "TRANSFORM Format(Sum(Items.amount),'#,##0.00') AS total SELECT Accounts.accCategory, Accounts.ID, Accounts.comment AS Account FROM Accounts INNER JOIN Items ON Accounts.ID = Items.accFrom WHERE (((Year([idate]))=2013) AND ((Items.category)<>3 Or (Items.category) Is Null) AND ((Accounts.accCategory)=6 OR (Accounts.accCategory)=7) AND ((Accounts.curr)=1)) GROUP BY Accounts.accCategory, Accounts.ID, Accounts.comment PIVOT Format(idate,'mmm') IN ('Jan','Feb','Mar','Apr', 'May','Jun','Jul','Aug','Sep','Oct','Nov','Dec')"
    dsmcmd = New OleDbDataAdapter(strSelect, cn)
    dsmcmd.Fill(dsm, "Totals")        
    dgMonths.ItemsSource = dsm.Tables("Totals").DefaultView

Thanks Andy

1

There are 1 answers

0
Andy Powell On BEST ANSWER

Figured it out.... rather complicated for something that should really be simple!

XAML

<Window.Resources>
    <l:DGAmountConverter x:Key="DGAmountConverter"/>
    <l:DGColourConverter x:Key="DGColourConverter"/>

    <DataTemplate x:Key="ColourAmount">
        <Grid>
            <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dg:DataGridCell}, Converter={StaticResource DGAmountConverter}}" Foreground="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dg:DataGridCell}, Converter={StaticResource DGColourConverter}}"/>
        </Grid>
    </DataTemplate>        
    <l:TemplateSelector x:Key="TemplateSelector" DefaultTemplate="{StaticResource ColourAmount}"/>

</Window.Resources>        

....

<l:AmountDataGrid x:Name="dgMonths" Style="{StaticResource MonthsView}" CellTemplateSelector="{StaticResource TemplateSelector}"/>

Code behind

Public Class AmountDataGrid
Inherits Microsoft.Windows.Controls.DataGrid

Public Property CellTemplateSelector() As DataTemplateSelector
    Get
        Return DirectCast(GetValue(CellTemplateSelectorProperty), DataTemplateSelector)
    End Get
    Set(value As DataTemplateSelector)
        SetValue(CellTemplateSelectorProperty, value)
    End Set
End Property

Public Shared ReadOnly CellTemplateSelectorProperty As DependencyProperty = DependencyProperty.Register("Selector", GetType(DataTemplateSelector), GetType(AmountDataGrid), New FrameworkPropertyMetadata(Nothing))

Protected Overrides Sub OnAutoGeneratingColumn(e As Microsoft.Windows.Controls.DataGridAutoGeneratingColumnEventArgs)
    e.Cancel = True
    Columns.Add(New Microsoft.Windows.Controls.DataGridTemplateColumn() With {.Header = e.Column.Header, .CellTemplateSelector = CellTemplateSelector})
End Sub
End Class

Public Class TemplateSelector
Inherits DataTemplateSelector

Private _defaultTemplate As DataTemplate
Public Property DefaultTemplate() As DataTemplate
    Get
        Return _defaultTemplate
    End Get
    Set(value As DataTemplate)
        _defaultTemplate = value
    End Set
End Property

Public Overrides Function SelectTemplate(item As Object, container As System.Windows.DependencyObject) As DataTemplate

    Return Me.DefaultTemplate

End Function
End Class

Class DGAmountConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert

    Dim cell As Microsoft.Windows.Controls.DataGridCell = TryCast(value, Microsoft.Windows.Controls.DataGridCell)
    If cell IsNot Nothing Then
        Dim colNum As Integer = cell.Column.DisplayIndex
        Dim cp As ContentPresenter = TryCast(cell.Content, ContentPresenter)
        Dim view As DataRowView = TryCast(cp.Content, DataRowView)
        If view IsNot Nothing Then
            Return TryCast(view.Row.ItemArray(colNum), Object)
        End If
    End If
    Return DependencyProperty.UnsetValue

End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack

    Return Nothing

End Function
End Class

Class DGColourConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert

    Dim cell As Microsoft.Windows.Controls.DataGridCell = TryCast(value, Microsoft.Windows.Controls.DataGridCell)
    If cell IsNot Nothing Then
        Dim colNum As Integer = cell.Column.DisplayIndex
        Dim cp As ContentPresenter = TryCast(cell.Content, ContentPresenter)
        Dim view As DataRowView = TryCast(cp.Content, DataRowView)
        If view IsNot Nothing Then
            Dim obj As Object = view.Row.ItemArray(colNum)
            If obj Is Nothing OrElse Val(obj) >= 0 Then
                Return New SolidColorBrush(Colors.White)
            Else
                Return New SolidColorBrush(Colors.Red)
            End If
        End If
    End If
    Return DependencyProperty.UnsetValue

End Function

Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack

    Return Nothing

End Function
End Class