I'm using Visual Studio 2013 to create a Windows App Store app. I created a ResourceDictionary
in a file called AllButtonShapes.xaml
to store the Paths for all of the shapes I use on my buttons. I added AllButtonShapes.xaml
to my app resources:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="AllButtonShapes.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Three of the paths defined are x:Key
'd as ShapeView_N
, ShapeView_AccInt
, and ShapeView_XPYR
.
I have a custom UserControl called DynamicButton
which users 3 of the paths.
<local:DynamicButton x:Name="Button_N_AccInt_XPYR" Grid.Row="1" Grid.Column="1"
Margin="126,82,158,56"
ShapeWhite="{StaticResource ShapeView_N}"
ShapeBlue="{StaticResource ShapeView_AccInt}"
ShapeOrange="{StaticResource ShapeView_XPYR}"/>
In the XAML designer, I see the button render correctly with those 3 paths. The DynamicButton xaml uses a Grid (x:Name="ShapeGrid"
). When one of the three Shapes is set on the DynamicButton (via the attribute shown above), the code adds that Shape as a child of the grid.
this.ShapeGrid.Children.Add(this.shapeBlue);
However, when I run the app, it crashes with the following error:
"Element is already the child of another element."
I'm not using any of the shapes anywhere else (still in initial testing, so I've only got one button on the screen). I have no idea what it could already be a child of. I debugged it and looked this.shapeBlue.Parent, but it was null.
a) Assuming the Path is already a child of another element, how do I remove it from that element so I can use it?
b) Is there another, more proper, way to define reusable paths so that I can use them on my buttons?
I can't just define the paths inline in a UserControl, because I need to use the same Shape in different places.
I'm not perfectly happy with the solution, but this is what I ended up doing:
I generated all of my Paths using the compact markup syntax:
Then in my
DynamicButton
UserControl, instead of passing the Shape/Path itself, I just pass the String resource:In my
DynamicButton
class, I've got a getter/setter forShapeStringWhite
: