0

My goal: I want to create a reusable UserControl called DynamicButton (reusable within this app, I don't need to use it in different projects) which displays 3 different shapes. Each instance of the button will have a different set of 3 shapes.

I've tried to define the Path shapes separately so they can be reused (I'll use Path1 in a DynamicButton on multiple Pages). I've been through a couple iterations trying to find something that works. At first I just tried to define the paths as resources and then pass those resources to my DynamicButton class so they could be added to the button's internal Grid, but it wouldn't work because the Path object was already considered to be elsewhere in the view hierarchy so it wouldn't let me put it into the DynamicButton's view hierarchy. (see WinRT XAML trying to use a Path defined in ResourceDictionary, but "Element is already the child of another element." for details).

So, then I decided to try to generate my paths using the compact shape markup syntax shown below. I figured I would just pass that as a string into my DynamicButton code and then I could use that string to programmatically create the Path. However, it doesn't appear there is anyway to give that compact syntax to a Path object in code. It only works in XAML.

Now I'm trying to use a templated control with bindings to try to get the shape path markup into the Path object.

I've got a string defined which includes the compact Path markup syntax to describe a shape:

<x:String x:Key="ShapeView_N">M 131.12,10.6 L 141.17,10.6 L 141.17,85.34 L 132.07,85.34 L 82.68,27.78 L 82.68,85.34 L 72.74,85.34 L 72.74,10.6 L 81.31,10.6 L 131.12,68.65 L 131.12,10.6 L 131.12,10.6</x:String>

I want to pass that to my custom UserControl (DynamicButton) so that my control can create a Path with it.

<local:DynamicButton x:Name="MyButton" 
    ShapeString="{StaticResource ShapeView_N}" 
/>

In the template for my DynamicButton, I have code like this:

<Path x:Name="PathWhite" Data="{TemplateBinding ShapeString}" Fill="White" />

If I put that ShapeView_N string directly into the Data attribute for a Path, that would be perfectly valid xaml and would compile just fine. Like this:

<Path x:Name="PathWhite" Data="M 131.12,10.6 L 141.17,10.6 L 141.17,85.34 L 132.07,85.34 L 82.68,27.78 L 82.68,85.34 L 72.74,85.34 L 72.74,10.6 L 81.31,10.6 L 131.12,68.65 L 131.12,10.6 L 131.12,10.6" Fill="White" />

However, when I try to get it put there using the TemplateBinding, my app crashes because Data expects a Path object instead of a string. :( However, if I put a string literal into Data in my xaml, that works just fine.

I feel like this shouldn't be THIS hard. I have some Path data. I want to have a custom UserControl which accepts 3 Paths in SOME format.. ANY format will do.

Help?

2
  • you can use the `x:Shared="false" attribute on the resource, Path you want to share across multiple controls.
    – sa_ddam213
    Aug 27, 2014 at 4:33
  • It looks like x:Shared is only available for WPF, but this is a Windows App Store app. Aug 27, 2014 at 5:05

1 Answer 1

0

You could use StringToPathGeometryConverter to convert your strings to geometries, but I'd use a DataTemplate instead. You would define DataTemplate resources that have a Path element in them with the geometry specified, then use these as ContentTemplate values of your buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.