Stephen Lee Parker
Stephen Lee Parker

Reputation: 1265

How can I get geometry information in C# from an XAML object?

given the following XAML code:

<Canvas Name="MainView">

    <Canvas Name="TriangleElement" Width="50" Height="50" Canvas.Left="110" Canvas.Top="100">
        <Canvas.RenderTransform>
            <RotateTransform CenterX="25" CenterY="25" Angle="0" />
        </Canvas.RenderTransform>
        <Path Stroke="#FF009600" StrokeThickness="1" Fill="#FF68E168">
            <Path.Data>
                M 0,0 L 50,0 50,50 Z
            </Path.Data>
        </Path>
    </Canvas>

    <Canvas Name="SquareElement" Width="50" Height="50" Canvas.Left="170" Canvas.Top="100">
        <Canvas.RenderTransform>
            <RotateTransform CenterX="25" CenterY="25" Angle="0" />
        </Canvas.RenderTransform>
        <Path Stroke="#FF005DFF" StrokeThickness="1" Fill="#FF98D0F8">
            <Path.Data>M 0,0 L 50,0 50,50 0,50 Z</Path.Data>
        </Path>
    </Canvas>

</Canvas>

How can I get the path data / geometry information in c# without naming it in the XAML? In the past I have created several UserControl's, created an interface to the objects, and pulled the info based on the Path name. In my current case I cannot use this approach.

Upvotes: 0

Views: 985

Answers (1)

Tim Rogers
Tim Rogers

Reputation: 21723

Not sure if I understand the question, but can't you use

((Path)TriangleElement.Children[0]).Data

And what are those Canvas elements for? They don't seem to be doing anything.

Why not:

<Path Name="SquareElement" Width="50" Height="50" Stroke="#FF005DFF" StrokeThickness="1" Fill="#FF98D0F8">
    <Path.RenderTransform>
        <RotateTransform CenterX="25" CenterY="25" Angle="60" />
    </Path.RenderTransform>
    <Path.Data>M 0,0 L 50,0 50,50 0,50 Z</Path.Data>
</Path>

Then you can get straight to your paths by name.

Upvotes: 1

Related Questions