【问题标题】:Tooltip shows different text on hovering over button content工具提示在悬停在按钮内容上时显示不同的文本
【发布时间】:2017-12-12 09:05:10
【问题描述】:

我的情况是我有按钮并将样式设置为该按钮的静态资源(此处为 CreateNewItemButtonStyle)。 我还有一个工具提示,每当我将鼠标悬停在此工具提示上时(此处为“SelectAllLines”),我都会显示一些文本。 当我将鼠标悬停在按钮区域上时,它会正确显示工具提示文本,不包括内容区域(我的意思是当我将鼠标悬停在内容“AL”上时,它会在工具提示上显示“AL”,它不应该这样做,它应该显示 整个按钮区域工具提示上只有“SelectAllLines”)。

我发现这是因为我使用的风格。

但是如何在我通过 ToolTip="Selected Lines" 设置的整个按钮区域的工具提示上使用相同的文本

<Button
    x:Name="AllLinesButtonX"
    Background="{StaticResource FlowPowderBlackBrush}"
    Click="AllLinesButtonX_OnClick"
    Command="{Binding AllLinesCommand}"
    Content="AL"
    MouseRightButtonUp="SelectGeometryToggleButton_OnMouseRightButtonUp"
    Style="{StaticResource CreateNewItemButtonStyle}"  //If i remove this line it stops showing "AL", just shows "SelectedAllLines" which is the correct behavior
    ToolTip="SelectAllLines"
</Button>

这是风格的关键:

 <Style x:Key="CreateNewItemButtonStyle" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True" />
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisualStyle}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="TextBlock.FontSize" Value="10" />
<Setter Property="HorizontalAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Border
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                SnapsToDevicePixels="True">
                <ContentPresenter
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    Content="{TemplateBinding Content}"
                    TextBlock.TextAlignment="Center" />
            </Border>
        </ControlTemplate>
    </Setter.Value>
</Setter>
<Style.Triggers>
    <Trigger Property="IsMouseOver" Value="True">
        <Setter Property="Background" Value="{StaticResource ActiveButtonBrush}" />
        <Setter Property="BorderBrush" Value="{StaticResource ActiveBorderBrush}" />
    </Trigger>
    <Trigger Property="IsPressed" Value="True">
        <Setter Property="Background" Value="{StaticResource ActiveButtonBrush}" />
        <Setter Property="BorderBrush" Value="{StaticResource ActiveBorderBrush}" />
    </Trigger>
    <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Cursor" Value="Arrow" />
    </Trigger>
</Style.Triggers>

【问题讨论】:

  • 我已经尝试了上面的代码,似乎没有任何问题..它工作正常..

标签: wpf xaml styles tooltip staticresource


【解决方案1】:

在您的按钮样式的ControlTemplate 中将IsHitTestVisible="false" 设置为ContentPresenter

更多信息:https://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible(v=vs.110).aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多