Edit

LengthConverter Class

Definition

Converts instances of other types to and from instances of a Double that represent an object's length.

public ref class LengthConverter : System::ComponentModel::TypeConverter
public class LengthConverter : System.ComponentModel.TypeConverter
type LengthConverter = class
    inherit TypeConverter
Public Class LengthConverter
Inherits TypeConverter
Inheritance
LengthConverter

Examples

The following example shows how to create and use an instance of the LengthConverter object. A custom method called ChangeLeft is defined, which converts the content of a ListBoxItem (defined in a separate Extensible Application Markup Language (XAML) file) to an instance of Double, and later into a String. This method passes the ListBoxItem to a LengthConverter object, which converts the ListBoxItem Content to an instance of Double. Notice that this value has already been converted to a String by using the ToString method. This value is then passed back to the SetLeft method and the GetLeft method of the Canvas in order to change the position of the text1 object.

private void ChangeLeft(object sender, SelectionChangedEventArgs args)
{
    ListBoxItem li = ((sender as ListBox).SelectedItem as ListBoxItem);
    LengthConverter myLengthConverter = new LengthConverter();
    Double db1 = (Double)myLengthConverter.ConvertFromString(li.Content.ToString());
    Canvas.SetLeft(text1, db1);
    String st1 = (String)myLengthConverter.ConvertToString(Canvas.GetLeft(text1));
    canvasLeft.Text = "Canvas.Left = " + st1;
}
Private Sub ChangeLeft(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
    Dim li As ListBoxItem = CType(CType(sender, ListBox).SelectedItem, ListBoxItem)
    Dim myLengthConverter As New LengthConverter
    Dim db1 As Double = CType(myLengthConverter.ConvertFromString(li.Content.ToString()), Double)
    Canvas.SetLeft(text1, db1)
    Dim st1 As String = CType(myLengthConverter.ConvertToString(Canvas.GetLeft(text1)), String)
    canvasLeft.Text = "Canvas.Left = " + st1
End Sub

Remarks

LengthConverter supports conversion to and from the following types: String, Decimal, Single, Double, Int16, Int32, Int64, UInt16, UInt32, and UInt64.

Constructors

Name Description
LengthConverter()

Initializes a new instance of the LengthConverter class.

Methods

Name Description
CanConvertFrom(ITypeDescriptorContext, Type)

Determines whether conversion is possible from a specified type to a Double that represents an object's length.

CanConvertTo(ITypeDescriptorContext, Type)

Determines whether conversion is possible to a specified type from a Double that represents an object's length.

ConvertFrom(ITypeDescriptorContext, CultureInfo, Object)

Converts instances of other data types into instances of Double that represent an object's length.

ConvertTo(ITypeDescriptorContext, CultureInfo, Object, Type)

Converts other types into instances of Double that represent an object's length.

Applies to