Use LengthConverter in WPF

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.

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