简体   繁体   English

如何从XAML对象获取C#中的几何信息?

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

given the following XAML code: 给出以下XAML代码:

<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? 如何在c#中获取路径数据/几何信息而无需在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. 过去,我创建了多个UserControl,创建了对象的接口,并根据路径名称提取了信息。 In my current case I cannot use this approach. 在目前的情况下,我无法使用这种方法。

Not sure if I understand the question, but can't you use 不知道我是否理解这个问题,但您不能使用

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

And what are those Canvas elements for? 这些Canvas元素是做什么用的? 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. 然后,您可以按名称直接进入路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF C#路径:如何从具有路径数据的字符串获取代码中的几何(不在XAML中) - WPF C# Path: How to get from a string with Path Data to Geometry in Code (not in XAML) C# - 如何从xaml resourcedictionary格式的字符串中获取密钥和值? - C# - How can I get the key and value from strings of xaml resourcedictionary format? 在C#中,如何从对象中获取字符串名称 - in C#, how can i get the string name from an object ¿如何使用 C# 获取网络有效载荷中的信息? - ¿How can i get information in Network Payload using C#? 如何从异常(随机发生 C# WPF)中获取更多信息? - How can I get more information from Exception (occurring randomly C# WPF)? 如何在C#中从OpenOffice获取性能信息? - How can I take the performance information from OpenOffice in C#? C#如何获取此信息? - C# How I can retrieve this information? 如何让WinRT InputScope在C#代码中工作,而不是XAML? - How can I get WinRT InputScope to work in C# code, not XAML? 如何获得将光标设置为组合框xaml / c#中的文本的功能? - How can I get the set the cursor to text in a combo-box xaml/c#? 如何使用C#获得xaml元素? - How can you get a xaml element using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM