代码之家  ›  专栏  ›  技术社区  ›  Sonny D

如何像父元素一样设置工具提示宽度?

wpf
  •  1
  • Sonny D  · 技术社区  · 6 年前

    如何设置 Tootip

    <Button ToolTipService.ShowOnDisabled="True" Content="Save" IsEnabled="False" Width="150">
        <Button.ToolTip>
            <ToolTip Placement="Top" VerticalOffset="-5">
                <TextBlock>Confirm before save.</TextBlock>
            </ToolTip>
        </Button.ToolTip>
    </Button>
    

    现在工具提示的宽度与文本的宽度相对应 'Confirm before save.'

    2 回复  |  直到 6 年前
        1
  •  1
  •   Alexandre Asselin    6 年前

    可以对工具提示的PlacementTarget属性进行绑定,如下所示:

        <Button x:Name="SaveButton" ToolTipService.ShowOnDisabled="True" Content="Save" IsEnabled="False" Width="150">
            <Button.ToolTip>
                <ToolTip Placement="Top" VerticalOffset="-5"  Width="{Binding PlacementTarget.Width, RelativeSource={RelativeSource Self}}">
                    <TextBlock>Confirm before save.</TextBlock>
                </ToolTip>
            </Button.ToolTip>
        </Button>
    
        2
  •  1
  •   Bakri Bitar    6 年前

    Tooltip 是包含它的元素的一个属性(并且它不是您前面提到的父元素)

    快速解决方法是 将宽度属性定义为资源 在您的窗口/页面中 然后在工具提示和包含元素中使用它

        <!--Define a width as resource-->
        <Window.Resources>
                 <sys:Double x:Key="WidthOf200">200</sys:Double>
         </Window.Resources>
        <Grid>
            <!--Use the same width in the button and its tooltip-->
            <Button Name="button1" ToolTipService.ShowOnDisabled="True" Content="Save" IsEnabled="False" Height="50"  Width="{StaticResource WidthOf200}">
                <Button.ToolTip>
                    <!--Use the same width in the button and its tooltip-->
    
                    <ToolTip Placement="Top" VerticalOffset="-5"  Width="{StaticResource WidthOf200}">
                        <TextBlock  >Confirm before save.</TextBlock>
                    </ToolTip>
                </Button.ToolTip>
            </Button>
        </Grid>
    </Window>
    

    enter image description here