Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

wpf - How to position tooltip bottom center

I'm trying to position my tooltip so that it would be on the bottom and center of my target object. I can position it to be just on the bottom by ToolTipService.Postion="Bottom", but how to position it to be also on the center?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I agree, the options available for positioning a ToolTip are a little limited. I think you'll have to combine Placement="Bottom" with HorizontalOffset to get Bottom/Center positioning.

To center the ToolTip relative to the PlacementTarget you can use
(PlacementTarget.ActualWidth / 2.0) - (ToolTip.ActualWidth / 2.0)

Example

<Button Content="Test">
    <Button.ToolTip>
        <ToolTip Content="ToolTip Text"
                 Placement="Bottom">
            <ToolTip.HorizontalOffset>
                <MultiBinding Converter="{StaticResource CenterToolTipConverter}">
                    <Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth"/>
                    <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth"/>
                </MultiBinding>
            </ToolTip.HorizontalOffset>
        </ToolTip>
    </Button.ToolTip>
</Button>

CenterToolTipConverter

public class CenterToolTipConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.FirstOrDefault(v => v == DependencyProperty.UnsetValue) != null)
        {
            return double.NaN;
        }
        double placementTargetWidth = (double)values[0];
        double toolTipWidth = (double)values[1];
        return (placementTargetWidth / 2.0) - (toolTipWidth / 2.0);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

If you need to center several ToolTips you could use a Style like

<Style x:Key="centeredToolTip" TargetType="ToolTip">
    <Setter Property="HorizontalOffset">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource CenterToolTipConverter}">
                <Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth"/>
                <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth"/>
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

<!-- ... -->

<Button Content="Test">
    <Button.ToolTip>
        <ToolTip Style="{StaticResource centeredToolTip}"
                 Placement="Bottom"
                 Content="ToolTip Text"/>
    </Button.ToolTip>
</Button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...