LengthConverter Class
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace: http://schemas.microsoft.com/winfx/2006/xaml/presentation
This example shows how to create and use an instance of the LengthConverter object. In this example, Canvas positioning values are represented by Content in a ListBoxItem. The Content is later converted into an instance of Double, which is a required argument for positioning. The value is then converted back into a String by using the LengthConverter. The value is displayed as text in a TextBlock element by using the GetLeft method.
This code is abbreviated. To view the complete sample, see the Positioning Properties of Canvas Sample.
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.
Public 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
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.