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:

http://blogs.msdn.com/silverlight_sdk/archive/2008/06/04/breaking-changes-for-silverlight-2-beta-2-corrections-and-additions.aspx

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=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Data=\"" + path_data+ "\"/>");

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

Tzanimir