Export (0) Print
Expand All

LengthConverter Class

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

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public class LengthConverter : TypeConverter
<LengthConverter .../>

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

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 Canvas Positioning Properties 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.

		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;
		}

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0

Community Additions

ADD
Show:
© 2014 Microsoft