Rob Perkins
Rob Perkins

Reputation: 3130

What is the correct XAML for a dynamic sized ToolTip?

I need to create a ToolTip for a label which is always twice the size of the Label. I have not been able to get the size of the ToolTip to be correct.

My code is here; I've included a complex template but the problem is the same if I use a string value:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="200"        Height="200"        Title="MainWindow">

    <Window.Resources>
        <ControlTemplate x:Key="Density">
            <StackPanel>
                <TextBlock  Text="𝑘𝑔" />
                <Separator Margin="2,0" BorderBrush="Black" />
                <TextBlock  Text="𝑚³" />
            </StackPanel>
        </ControlTemplate>

        <TransformGroup x:Key="DoubleSize">
            <ScaleTransform CenterX="0.5" CenterY="0.5"  ScaleX="2" ScaleY="2" />
            <SkewTransform />
            <RotateTransform />
            <TranslateTransform />
        </TransformGroup>

        <Style x:Key="TTipStyle" TargetType="ToolTip">
            <Setter Property="RenderTransform" Value="{StaticResource DoubleSize}"/>
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="HasDropShadow" Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate >
                        <Grid Height="{TemplateBinding Height}" Width="{TemplateBinding Width}" Background="AliceBlue">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Label HorizontalAlignment="Center" VerticalAlignment="Center" Template="{StaticResource Density}">
            <Label.ToolTip>
                <ToolTip RenderTransform="{StaticResource DoubleSize}" Background="AliceBlue" Style="{StaticResource TTipStyle}" Template="{StaticResource Density}" />
            </Label.ToolTip>
        </Label>
        <ContentControl HorizontalAlignment="Left" VerticalAlignment="Top" Template="{StaticResource Density}" RenderTransform="{StaticResource DoubleSize}"/>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Text="::---- THIS SIZE TOOLTIP"/>
    </Grid>

</Window>

Needs to display the whole element, double sized

How should I define a Style for the ToolTip which has size extents that are large enough to contain the full, double-sized label content?

Upvotes: 0

Views: 57

Answers (1)

Mark Feldman
Mark Feldman

Reputation: 16148

Take your RenderTransform out of the ToolTip tag itself...

<ToolTip Background="AliceBlue" Style="{StaticResource TTipStyle}" Template="{StaticResource Density}" />

...and then in your style remove the RenderTransform and Template and set the LayoutTransform instead:

<Style x:Key="TTipStyle" TargetType="ToolTip">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="HasDropShadow" Value="True" />
    <Setter Property="LayoutTransform">
        <Setter.Value>
            <ScaleTransform ScaleX="2" ScaleY="2" />
        </Setter.Value>
    </Setter>
</Style>

Upvotes: 1

Related Questions