2

I have a XAML path as

<Path Width="30" Height="40" Stretch="Fill" Fill="Red" Data="F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z "/>

How do I covert it into the code in C# such as:

public static Grid Icon()
{
    Grid mygrid = new Grid();

    string thedata = "F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z";

    Path path = new Path();
    path.Height = 40;
    path.Width = 30;
    path.Fill = new SolidColorBrush(Colors.Red);
    path.SetValue(System.Windows.Shapes.Path.DataProperty, thedata);

    mygrid.Children.Add(path);

    return mygrid;
}

The code above having problem. any suggestion?

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Snake Chia
  • 181
  • 1
  • 3
  • 13
  • 1
    What's the problem? Do you have an exception message? – codemonkeh Oct 12 '13 at 13:38
  • this is the error message: [code]DependencyProperty of type System.Windows.Media.Geometry cannot be set on an object of type System.String.[/code] – Snake Chia Oct 12 '13 at 13:53
  • Duplicate of http://stackoverflow.com/questions/2029680/wpf-c-sharp-path-how-to-get-from-a-string-with-path-data-to-geometry-in-code-n. In there, you also have an (accepted) answer which should work and help you. – Per Lundberg Oct 12 '13 at 15:50

1 Answers1

1

You are getting this issue because "thedata" is a string.

To fix this issue, use the Geometry class to parse "thedata" string:

    public static Grid Icon()
    {
        Grid mygrid = new Grid();

        string thedata = "F1 M 36.4167,19C 44.2867,19 50.6667,24.6711 50.6667,31.6667C 50.6667,32.7601 50.5108,33.8212 50.2177,34.8333L 36.4167,57L 22.6156,34.8333C 22.3225,33.8212 22.1667,32.7601 22.1667,31.6667C 22.1667,24.6711 28.5466,19 36.4167,19 Z M 36.4167,27.7083C 34.2305,27.7083 32.4583,29.4805 32.4583,31.6667C 32.4583,33.8528 34.2305,35.625 36.4167,35.625C 38.6028,35.625 40.375,33.8528 40.375,31.6667C 40.375,29.4805 38.6028,27.7083 36.4167,27.7083 Z";

        Path path = new Path();
        path.Height = 40;
        path.Width = 30;
        path.Fill = new SolidColorBrush(Colors.Red);
        path.Data = Geometry.Parse(thedata);

        mygrid.Children.Add(path);

        return mygrid;
    }
Ben Smith
  • 19,589
  • 6
  • 65
  • 93
  • Not quite right. DataProperty is not the expected *Type*, it is the name of the dependency property. It is expected to be of the Geometry type. – Per Lundberg Oct 12 '13 at 15:51
  • Doh. Yes updated my answer to include necessary string converter. – Ben Smith Oct 12 '13 at 15:58
  • the PathConverter.StringToPathGeometryConverter() wasn't exist in Windows Phone. :( – Snake Chia Oct 13 '13 at 02:17
  • @shawchyn I've finally had a chance to recreate your issue in VStudio. Try the solution I've given above. – Ben Smith Oct 13 '13 at 16:53
  • @Fresh I'm working on the code for Windows Phone 8, it seem to be "Parse" is not available for System.Windows.Media.Geometry". As the result, I can't create the Geometry. :( I'd to find another way to work around it. :( – Snake Chia Oct 16 '13 at 02:59
  • @shawchyn I assumed these methods would be available in Windows Phone 8 envirnoment! Instead try using the binding mechanism shown [here](http://stackoverflow.com/a/16227344/203371). – Ben Smith Oct 16 '13 at 14:07
  • @shawchyn as this method works can you accept it as the answer? This will help others with the same problem too. – Ben Smith Mar 14 '14 at 23:25