0

I have a XAML Path...

<Path x:Name="test" Stretch="Uniform" Fill="#FF2A2A2A" Data="F1 M 9143.18... Z "/>

I would like to change the "Data" by C# like this (I have the new path data as string)...

test.Data = "F1 M 987... Z";

How I can achive this for an universal app (8.1)?

This should work but it doesn't (Windows.UI.Xaml.Media.Geometry contains no definition for 'Parse')

test.Data = Geometry.Parse("F1 M 987... Z");

Any Help or Guidance with this would be appreciated.

Thankyou in Advance.

Ok Thank you ... here is how it works for me...

<Path x:Name="test" Stretch="Uniform" Fill="#FF2A2A2A" Data="{Binding}"/>

test.DataContext = "F1 M 987... Z";
user3168511
  • 254
  • 1
  • 15

2 Answers2

2

If you want to parse a string to a Geometry you can use XamlReader.Load. Once you have the Geometry then you can bind it as BCdotNet suggests (but you may want to fire a change notification) or set the Data explicitly

// Parse the path-format string into a Geometry
Geometry StringToPath(string pathData)
{
    string xamlPath = 
        "<Geometry xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" 
        + pathData + "</Geometry>";

    return Windows.UI.Xaml.Markup.XamlReader.Load(xamlPath) as Geometry;
}

// In some function somewhere...
// Set the path either directly
test.Data = StringToPath("M 282.85714,355.21933 160,260.93361 260,169.50504 448.57143,163.79075 494.28571,286.6479 z");
// Or via data binding
PathGeometry = StringToPath("M 282.85714,355.21933 160,260.93361 260,169.50504 448.57143,163.79075 494.28571,286.6479 z");
//....

// If we want changes to the bound property to take effect we need to fire change notifications
// Otherwise only the initial state will apply
Geometry _geometry;
public Geometry PathGeometry
{
    get
    {
        return _geometry;
    }
    set
    {
        _geometry = value;
        NotifyPropertyChanged("PathGeometry");
    }
}
Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
1

You will need to Bind it.

C#:

public PathGeometry Geometry
{
   get
   {
      return pathGeometry ;
   }
}

XAML:

<Path Data="{Binding Path=chartmaker.Geometry}"/>
Community
  • 1
  • 1
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35