0

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!

GilShalit
  • 6,175
  • 9
  • 47
  • 68
  • Does a breakpoint in your `OnAutoGeneratingColumn` handler get hit? – Sheridan Jan 20 '14 at 09:06
  • Yes, a breakpoint shows that the code I posted from OnAutoGeneratingColumn does run, and that dgtc.ElementStyle setters includes a setter with Property = Text – GilShalit Jan 20 '14 at 11:30
  • Have you tried setting `UsageBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;`? Maybe it *is* working, but just not *when* expected? – Sheridan Jan 20 '14 at 11:34
  • @Sheridan, tried this now, no change. I mean the Converter to be called when the data is loaded into the column. – GilShalit Jan 20 '14 at 11:42

2 Answers2

2

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
Sheridan
  • 68,826
  • 24
  • 143
  • 183
0

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
potehin143
  • 541
  • 4
  • 9