Question

In an autogenerated datagrid column, I want to replace a certain value (-1) with blank.

I created an IValueConverter:

<ValueConversion(GetType(DataRowView), GetType(String))>
Public Class UsageConversion
    Implements IValueConverter

    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
        Dim model As ProgModel = DirectCast(value, ProgModel)
        If model.Usage = -1 Then
            Return ""
        Else
            Return model.Usage.ToString
        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
        Return Nothing
    End Function
End Class

And bound it into the OnAutoGeneratingColumn method:

        Dim dgtc As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
        If dgtc IsNot Nothing Then 
                Dim UsageBinding = New Binding()
                UsageBinding.Converter = New UsageConversion
                Dim tbStyle As New Style
                tbStyle.TargetType = GetType(TextBlock)
                tbStyle.Setters.Add(New Setter(TextBlock.TextProperty, UsageBinding))
                dgtc.ElementStyle = tbStyle
        End If 

The OnAutoGeneratingColumn runs well, but the Convert code does not run - setting a breakpoint in it shows it is never called.

Any ideas why?

I am using .Net 4.0

Thanks!

Was it helpful?

Solution 2

I Guess you have to set UsageBinding.Path property

Binding.Path property it not set so you column it binded to nothing and that is why no value comes to Converter

try this

Dim UsageBinding = New Binding(e.PropertyName)
UsageBinding.Converter = New UsageConversion
dgtc.Binding=UsageBinding

OTHER TIPS

I'm not sure why you're trying to set this Binding on the ElementStyle... surely, you just want to set that Binding on the whole column:

    Dim dgtc As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
    If dgtc IsNot Nothing Then 
            Dim UsageBinding = New Binding()
            UsageBinding.Converter = New UsageConversion
            dgtc.Binding = UsageBinding
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top