Apply ValueConverter on DataGrid Column

4.8k views Asked by At

I have written a converter class(implementing IValueConverter) which converts a code that comes from the database (for example "CTY") into a more user friendly description (for example "City"). I want to use the converter on a single Column in my XCeed WPF Datagridcontrol, but I do not know to which property I must set the Converter to. I also tried to attach it to a DataCell using a style but it won't work properly and I think it is also not necessary since the converter should apply to only one column and not every cell.

The columns are also autogenerated so if I could apply it at runtime, that would be awesome!

I don't know to which property of the column I must aplly the converter to (the Xceed Column doens't have a "Binding" property. Do you guys have any suggestions?

More examples or code can be provided if asked for. I hope my problem is a bit clear for you.

EDIT:

This are the things I use in my XAML file:

     <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>

This is my Converter:

 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:

I tried with the DataGridItemProperty (see XAML below) but when I debug I do not enter in the Converter class and the grid just loads with the initial data and not with the converted data.

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

It definitely knows the BudgettaireEntiteit field because if I enter a field that doesn't exist it throws an error. Now it just does nothing

2

There are 2 answers

3
Adrian Fâciu On

You could use a DataGridCollectionViewSource with DataGridItemProperty. This object has a Converter property that can be used to assign the converter that you've created to the required column.

Something like:

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

More detailed example in exceed documentation.

0
SezMe On

A complete example code follows. This XAML applies a DateConverter to two date columns (DateDue and DatePaid) in an Xceed grid:

<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>