Infragistics(R) NetAdvantage(R) : Silverlight 2011.2
ValueToTextConverter Property
See Also  Example E-mail your feedback on this topic.
Infragistics.Controls.Editors Namespace > ValueInput Class : ValueToTextConverter Property

Specifies the converter used for converting between text and value.

Syntax

Visual Basic (Declaration) 
Public Property ValueToTextConverter As IValueConverter
C# 
public IValueConverter ValueToTextConverter {get; set;}

Example

The following code demonstrates how to create two custom value to text converters. In C# and VB code the converters implement IValueConverter on MyPointConverter and MyDisplayPointConverter. In XAML instances of these converters are assigned for the ValueToTextConverter and ValueToDisplayTextConverter properties of ValueInput. With the following code, the XamMaskedInput will accept Point objects via it's Value property and it will also be able to parse user input in the form of two integer values separated by comma (',') into Point object which will be returned by the Value property. Note: Make sure to define correct namespaces in the XAML to be able to refer to classes correctly in XAML and to set a correct mask according to the value type that you set for Value.

C#Copy Code
// Set the ValueType of the xamMaskedInput to Point
this.maskedInput.ValueType = typeof(Point);
VB.NETCopy Code
' Set the ValueType of the xamMaskedInput to Point
Me.maskedInput.ValueType = GetType(Point)
C#Copy Code
public class MyPointConverter:IValueConverter
    {
        object  IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ValueInput input = parameter as ValueInput;
            if (value is Point && targetType == typeof(string))
                return value.ToString();
            else return String.Empty;
        }

        object  IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value.ToString() != String.Empty && isPoint(value.ToString()))
                return Point.Parse(value.ToString());
            else return null;
        }

        // Returns true if text has the format "int,int"
        private bool isPoint(string text)
        {
            bool result = false;
            int tempInt;
            string[] point = new string[2];
            if (text.Contains(","))
            {
                point = text.Split(new char[1] { ',' }, 2);
            }

            foreach (string s in point)
            {
                result = int.TryParse(s, out tempInt);
            }
            return result;
        }
    }
VB.NETCopy Code
Public Class MyPointConverter
	Implements IValueConverter
	Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
		Dim input As ValueInput = TryCast(parameter, ValueInput)
		If TypeOf value Is Point AndAlso targetType = GetType(String) Then
			Return value.ToString()
		Else
			Return [String].Empty
		End If
	End Function

	Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
		If value IsNot Nothing AndAlso value.ToString() <> [String].Empty AndAlso isPoint(value.ToString()) Then
			Return Point.Parse(value.ToString())
		Else
			Return Nothing
		End If
	End Function

	' Returns true if text has the format "int,int"
	Private Function isPoint(text As String) As Boolean
		Dim result As Boolean = False
		Dim tempInt As Integer
		Dim point As String() = New String(1) {}
		If text.Contains(",") Then
			point = text.Split(New Char(0) {","C}, 2)
		End If

		For Each s As String In point
			result = Integer.TryParse(s, tempInt)
		Next
		Return result
	End Function
End Class
C#Copy Code
public class MyDisplayPointConverter : IValueConverter
    {
        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ValueInput input = parameter as ValueInput;
            if (value is Point && targetType == typeof(string))
                return "X: " + ((Point)value).X + ", Y: " + ((Point)value).Y;
            else return String.Empty;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        // Returns true if text has the format "int,int"
        private bool isPoint(string text)
        {
            bool result = false;
            int tempInt;
            string[] point = new string[2];
            if (text.Contains(","))
            {
                point = text.Split(new char[1] { ',' }, 2);
            }

            foreach (string s in point)
            {
                result = int.TryParse(s, out tempInt);
            }
            return result;
        }
    }
VB.NETCopy Code
Public Class MyDisplayPointConverter
	Implements IValueConverter
	Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
		Dim input As ValueInput = TryCast(parameter, ValueInput)
		If TypeOf value Is Point AndAlso targetType = GetType(String) Then
			Return ("X: " + DirectCast(value, Point).X & ", Y: ") + DirectCast(value, Point).Y
		Else
			Return [String].Empty
		End If
	End Function

	Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
		Throw New NotImplementedException()
	End Function

	' Returns true if text has the format "int,int"
	Private Function isPoint(text As String) As Boolean
		Dim result As Boolean = False
		Dim tempInt As Integer
		Dim point As String() = New String(1) {}
		If text.Contains(",") Then
			point = text.Split(New Char(0) {","C}, 2)
		End If

		For Each s As String In point
			result = Integer.TryParse(s, tempInt)
		Next
		Return result
	End Function
End Class
XAMLCopy Code
<ig:XamMaskedInput x:Name="maskedInput" Mask="nn\,nn">
     <ig:XamMaskedInput.ValueToTextConverter>
                    <local:MyPointConverter/>
                </ig:XamMaskedInput.ValueToTextConverter>

                <ig:XamMaskedInput.ValueToDisplayTextConverter>
                    <local:MyDisplayPointConverter/>
      </ig:XamMaskedInput.ValueToDisplayTextConverter>
</ig:XamMaskedInput>

Remarks

The conversions between the 'string' and the ValueType by default are done using built in conversion logic. You can override the conversion logic by setting the ValueToDisplayTextConverter and ValueToTextConverter. ValueToTextConverter is used when the editor is in edit mode where as ValueToDisplayTextConverter is used when the editor is not in edit mode.

Note: An editor can edit values of types other than 'string'. For example, a XamTextEditor can edit values of types DateTime. You can specify the type of values being edited by the editor using the Infragistics.Controls.Editors.ValueInput.ValueType property.

Although the built-in default conversion logic should be sufficient for most situations, you may want make use of this functionality to provide custom logic for converting user input into value type object. Examples where this would be needed are if you are editing custom objects where the built-in conversion logic would not know how to convert text into custom object type. Or you want to support entering certain symbols in the text that signify certain aspect of the value - for example you want 'k' in '2k' to be interpreted as 1000 magnitude, or +1d to be interpreted as tomorrow's date when editing DateTime.

See Also