Hi,
try this code. Your code doesn't work. Check this code:
PropertyMetadata(String.Empty)
Return value.ToString().Substring(0, If(Me.ConverterParameter Is Nothing, value.ToString().Length, Math.Min(CInt(Me.ConverterParameter), value.ToString().Length)))
Public Class MySubstringConverter
Inherits DependencyObject
Implements IValueConverter
Public Shared ReadOnly ConverterParameterProperty As DependencyProperty =
DependencyProperty.RegisterAttached("ConverterParameter", GetType(String),
GetType(MySubstringConverter), New PropertyMetadata(String.Empty))
Public Shared Function GetConverterParameter(obj As DependencyObject) As String
obj.GetValue(ConverterParameterProperty).ToString()
End Function
Public Shared Sub SetConverterParameter(obj As DependencyObject, value As String)
obj.SetValue(ConverterParameterProperty, value)
End Sub
Public Property ConverterParameter As Object
Get
Return GetValue(ConverterParameterProperty)
End Get
Set(value As Object)
SetValue(ConverterParameterProperty, value)
End Set
End Property
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Return If(value Is Nothing, "", value.ToString()) & " - " & If(Me.ConverterParameter Is Nothing, "", Me.ConverterParameter.ToString())
'Return value.ToString().Substring(0, If(Me.ConverterParameter Is Nothing, value.ToString().Length, Math.Min(CInt(Me.ConverterParameter), value.ToString().Length)))
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
Try this Convert function:
Public Function Convert(value As Object, targetType As Type, parameter As Object, language As String) As Object Implements IValueConverter.Convert
Dim v = value.ToString
Dim l1 = v.Length
Dim l2 As Integer = 0
If Me.ConverterParameter IsNot Nothing AndAlso Integer.TryParse(Me.ConverterParameter.ToString, l2) Then
l1 = Math.Min(l2, l1)
End If
Return v.Substring(0, l1)
End Function