无法获得正确的标签尺寸

时间:2011-04-01 18:51:47

标签: wpf

我需要编写一个控件,如下所示:

Click Here to se callout

问题是我无法获得标签的实际尺寸来重绘我的矩形几何体。标签的高度总是远大于它在屏幕上占据的空间。我不知道,该怎么做。这是代码:

<Popup x:Class="Controls.Callout"
        x:ClassModifier="internal"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
    <Grid>
        <Image>
            <Image.Source>
                <DrawingImage>
                    <DrawingImage.Drawing >
                        <GeometryDrawing Brush="Orange" x:Name="geometryDrawing">
                            <GeometryDrawing.Pen>
                                <Pen Brush="Black" Thickness="2"/>
                            </GeometryDrawing.Pen>
                            <GeometryDrawing.Geometry>
                                <CombinedGeometry GeometryCombineMode="Union">
                                    <CombinedGeometry.Geometry1>
                                        <RectangleGeometry x:Name="rectangel" 
                                                                   RadiusX="15" RadiusY="15" 
                                                                       Rect="0,30, 300,100"
                                                                   />
                                    </CombinedGeometry.Geometry1>
                                    <CombinedGeometry.Geometry2>
                                        <PathGeometry>
                                            <PathFigure StartPoint="30,30" IsClosed="False">
                                                <PolyLineSegment Points="15,0, 90,30"/>
                                            </PathFigure>
                                        </PathGeometry>
                                    </CombinedGeometry.Geometry2>
                                </CombinedGeometry>
                            </GeometryDrawing.Geometry>
                        </GeometryDrawing>
                    </DrawingImage.Drawing>
                </DrawingImage>
            </Image.Source>
       </Image>
        <Label Padding="5 20"  MaxWidth="300" Name="myLabel" FontSize="16" >
                   <!--Content="{Binding}">-->
               <!--MaxHeight="100" MaxWidth="300">-->
            <AccessText TextWrapping="Wrap" MaxHeight="50"/>
        </Label>
    </Grid>
</Popup>

代码背后:

internal partial class Callout : Popup
{
    public Callout()
    {
        InitializeComponent();
    }
    protected override void OnOpened(System.EventArgs e)
    {
          rectangel = new RectangleGeometry(new Rect(0,30,300, myLabel.Height/2));
    }
}

1 个答案:

答案 0 :(得分:1)

为什么不使用带有圆角矩形的Grid,TextBlock和矩形顶部的某些形状来使标注“指针”?作为网格,矩形可以自动扩展到TextBlock所需的完整大小。

例如,您可以创建一个UserControl(或一个成熟的模板控件),并为其指定MaxWidth,以便文本换行。然后将控件放在Canvas中,以便它可以确定自己的大小。

<UserControl MaxWidth="200">
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Rectangle Grid.Row="1"  RadiusX="50" RadiusY="50" 
             StrokeThickness="8" Stroke="Gray" />
        <!-- Here will be pointer in Grid.Row="0"-->
        <TextBlock Grid.Row="1" Name="myLabel" Margin="20" Foreground="Black" 
             Text="This is the textblock....." FontSize="20" TextWrapping="Wrap" />

    </Grid>
</UserControl>