在DataGrid列上应用ValueConverter

时间:2012-01-20 09:02:51

标签: wpf vb.net xaml ivalueconverter xceed-datagrid

我编写了一个转换器类(实现IValueConverter),它将来自数据库的代码(例如“CTY”)转换为更加用户友好的描述(例如“City”)。我想在我的XCeed WPF Datagridcontrol中的单个列上使用转换器,但我不知道我必须将Converter设置为哪个属性。我也尝试使用样式将它附加到DataCell但它无法正常工作,我认为它也没有必要,因为转换器应该只适用于一列而不是每个单元格。

列也是自动生成的,所以如果我可以在运行时应用它,那就太棒了!

我不知道列的哪个属性我必须转换为(Xceed Column没有“绑定”属性。你们有什么建议吗?

如果要求,可以提供更多示例或代码。我希望我的问题对你来说有点清楚。

编辑:

这是我在XAML文件中使用的内容:

     <utils:BudgettaireEntiteitConverter x:Key="BudgettaireEntiteitConverter" />

    <xcdg:DataGridCollectionViewSource x:Key="GridViewSourceDefault"
                                               Source="{Binding Converter={StaticResource BudgettaireEntiteitConverter}}">
                <xcdg:DataGridCollectionViewSource.DetailDescriptions>
                        <lc:ActieOverzichtBudgettenDescription  
                                                         RelationName="Budgetten"
                                                         AutoCreateDetailDescriptions="False" 
                                                         AutoCreateForeignKeyDescriptions="False"
                                                         AutoCreateItemProperties="True"
                                                         Title="Budgetten" >
                            <lc:ActieOverzichtBudgettenDescription.StatFunctions>
                                <xcdg:SumFunction ResultPropertyName="SumOfBedragInBudget"
                                              SourcePropertyName="BedragInBudget" />

                                <xcdg:SumFunction ResultPropertyName="SumOfBedragInAfwachting"
                                              SourcePropertyName="BedragInAfwachting" />
                            </lc:ActieOverzichtBudgettenDescription.StatFunctions>

                        <lc:ActieOverzichtBudgettenDescription.DetailDescriptions>
                                <lc:ActieBudgetRegistratieSleutelsDescription RelationName="RegistratieSleutels"
                                                                              AutoCreateDetailDescriptions="False"
                                                                              AutoCreateForeignKeyDescriptions="False"
                                                                              AutoCreateItemProperties="True"
                                                                              Title="Registratiesleutels" />
                </lc:ActieOverzichtBudgettenDescription.DetailDescriptions>

         </lc:ActieOverzichtBudgettenDescription>

   </xcdg:DataGridCollectionViewSource.DetailDescriptions>

</xcdg:DataGridCollectionViewSource>

 <xcdg:DataGridControl x:Name="lsvActies"
                              TargetUpdated="OnListTargetUpdated"
                              ItemsSourceName="Acties" 
                              IsRefreshCommandEnabled="False"
                              rf:XceedGridService.LoadUserSettings="True"
                              rf:XceedGridService.SettingsKeyName="ActieOverzichtGridKey"
                              rf:XceedGridService.ItemContextMenu="{StaticResource ActieContextMenu}">

                <xcdg:DataGridControl.CommandBindings>
                <CommandBinding Command="Delete" Executed="ExecuteDeleteItem" CanExecute="CanExecuteDeleteItem"/>
            </xcdg:DataGridControl.CommandBindings>

        </xcdg:DataGridControl>

这是我的转换器:

 Public Class BudgettaireEntiteitConverter
        Implements IValueConverter

        Private hs As Hashtable = FillHashTable()


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

            If hs.ContainsKey(value)
                Return hs(value).ToString()
            Else
                Return Nothing
            End If

        End Function

        Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
            Throw New NotSupportedException("ConvertBack not supported!")
        End Function

        Function FillHashTable() As Hashtable
            Dim hashtable As New Hashtable
            Dim dataCache = New ReferentieDataCache

            Dim budgettaireEntiteiten = dataCache.GetBudgettaireEntiteiten()

            For Each budgettaireEntiteitRow As BudgettaireEntiteitRow In budgettaireEntiteiten
                hashtable.Add(budgettaireEntiteitRow.BudgettaireEntiteit, budgettaireEntiteitRow.DisplayOmschrijving)
            Next

            Return hashtable
        End Function

    End Class

EDIT2:

我尝试使用DataGridItemProperty(参见下面的XAML),但是当我调试时,我没有进入Converter类,网格只加载初始数据而不加载转换后的数据。

 <xcdg:DataGridCollectionViewSource.ItemProperties>
        <xcdg:DataGridItemProperty Name="BudgettaireEntiteit" Converter="{StaticResource BudgettaireEntiteitConverter}" />
 </xcdg:DataGridCollectionViewSource.ItemProperties>

它肯定知道BudgettaireEntiteit字段,因为如果我输入一个不存在的字段,则会抛出错误。现在它什么都不做

2 个答案:

答案 0 :(得分:4)

您可以将DataGridCollectionViewSource与DataGridItemProperty一起使用。此对象具有Converter属性,可用于将您创建的转换器分配给所需的列。

类似的东西:

 <xcdg:DataGridItemProperty Name="RequiredColumn"
       Converter="{StaticResource BudgettaireEntiteitConverter}"/>

exceed documentation中的更详细示例。

答案 1 :(得分:0)

下面是完整的示例代码。此XAML将DateConverter应用于Xceed网格中的两个日期列(DateDue和DatePaid):

<Window.Resources>
    <local:DateConverter x:Key="conDate" />
    <xcdg:DataGridCollectionViewSource
        AutoCreateItemProperties="False"
        Filter="Filter_By_Member"
        x:Key="cvsDonations">
        <xcdg:DataGridCollectionViewSource.ItemProperties>
            <xcdg:DataGridItemProperty
                Name="DateDue"
                Converter="{StaticResource conDate}">
            </xcdg:DataGridItemProperty>
            <xcdg:DataGridItemProperty
                Name="DatePaid"
                Converter="{StaticResource conDate}">
            </xcdg:DataGridItemProperty>
        </xcdg:DataGridCollectionViewSource.ItemProperties>
    (</xcdg:DataGridCollectionViewSource>

.....

</Window.Resources>