WPF:如何在XAML中访问路径的渲染尺寸?

时间:2010-09-15 17:06:09

标签: wpf xaml path canvas

我正在尝试将文字内容叠加在Path描述的区域之上。 Path位于ViewBox包裹的Canvas中,因此可以缩放到任意大小。 TextBlock位于Canvas包装的网格中,尝试使用“路径”维度来确定文本块的位置。

<?xml version="1.0" encoding="UTF-8"?>
<Canvas
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Width="1299" Height="413">
    <ContentControl Width="300" Height="Auto">
        <Grid>
            <Viewbox Stretch="Uniform" x:Name="MyViewBox">
                <Canvas Width="121.664" Height="79.158" x:Name="MyCanvas">
                    <Canvas.LayoutTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="1" ScaleY="1"/>
                        </TransformGroup>
                    </Canvas.LayoutTransform>
                    <Path x:Name="MyPath" Width="115.666" Height="54.334" Canvas.Left="2.998" Canvas.Top="3.086" Data="M 115.666015,46.333496 C 115.666015,50.733887 112.066406,54.333985 107.666015,54.333985 L 7.99999999999994,54.333985 C 3.59960900000004,54.333985 0,50.733887 0,46.333496 L 0,8 C 0,3.60058600000002 3.59960900000004,0 7.99999999999994,0 L 107.666015,0 C 112.066406,0 115.666015,3.60058600000002 115.666015,8 L 115.666015,46.333496 Z">
                        <Path.Fill>
                            <SolidColorBrush Color="Red"/>
                        </Path.Fill>
                    </Path>
                </Canvas>
            </Viewbox>
            <Canvas>
                <Grid Width="115.666" Height="54.334" Canvas.Left="2.998" Canvas.Top="3.086" Margin="10,10">
                    <Thumb/>
                    <TextBlock TextWrapping="Wrap">
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
                    </TextBlock>
                </Grid>
            </Canvas>
        </Grid>
    </ContentControl>
</Canvas>

出现两个问题。首先,Grid使用Path的声明尺寸,而不是最终渲染的尺寸。此外,如果我在路径画布上将ScaleTransform.ScaleY设置为“-1”(以垂直镜像路径),则TextBlock不会重定位到路径的新位置。

是否可以在XAML中执行此操作,还是必须使用代码隐藏来在运行时读取位置?

编辑:示例网格实际上并没有引用路径属性,因为我无法确定要引用的正确属性。

1 个答案:

答案 0 :(得分:0)

您可以绑定到Path的{​​{1}}和ActualWidth,如下所示:

ActualHeight

HTH