Seekeer
Seekeer

Reputation: 1

Cant get right label size

I need to write a control, which will look like this:

Click Here to se callout

Problem is that i cant get label's real size to redraw my rectangle geometry. Label's height is always much bigger then space that it really occupies on the screen. I dont know, what to. Here is code:

<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>

Code behind:

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));
    }
}

Upvotes: 0

Views: 380

Answers (1)

foson
foson

Reputation: 10227

Why not use a Grid with a Rectangle with rounded corners, a TextBlock, and some shape on top of the rectangle to make the callout "pointer"? As a Grid, the rectangle can automatically expand to the full size needed by the TextBlock.

For example, you can create a UserControl (or a full-fledged templated control) and given it a MaxWidth so that the text wraps. Then put the control in a Canvas so that it can determine its own size.

<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>

Upvotes: 1

Related Questions