0

我正在使用 WpfToolkit for .NET 3.5 中的 DataGrid。我正在显示数据表。我希望负数为红色。列数是可变的,因此 AutoGenerateColumns 为 True。

按照这里的建议http://wpftutorial.net/DataGrid.html我做了以下事情。但是,每当调用 TemplateSelector 时,item 都是 DataRowView 而不是单元格内容。如何引用单元格内容?

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

XAML 中的数据网格

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

选择器

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

绑定数据表的代码

    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

谢谢安迪

4

1 回答 1

0

想通了....对于本来应该很简单的事情来说相当复杂!

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}"/>

背后的代码

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
于 2014-01-05T23:58:46.637 回答