Wednesday, 11 June 2008

Silverlight 2.0 Beta 2 : Set the Data property of a Path (Path.Data)

After Upgrading from Silverlight 2.0 Beta 1 to Beta2, there we several things that no more worked. Most of the Issues are covered in the Breaking Changes:

One of the Issues that I did not find answer for is that the following code:

String path_data = "M0,0L1,1";
Path path = new Path();
path.SetValue(Path.DataProperty, path_data);

now throws the exception:

"DependencyProperty of type System.Windows.Media.Geometry cannot be set on an object of type System.String"

The workaroud that I found is the follwing construction:

using System.Windows.Markup;

String path_data = "M0,0L1,1";
Path path = (Path)XamlReader.Load("<Path xmlns=\"\" Data=\"" + path_data+ "\"/>");

If you need you can also create only temporary Path object and take the PathGeometry object from the Data property;