wpf:当按钮被命令禁用时如何显示工具提示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4153539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:17:53  来源:igfitidea点击:

wpf: how to show tooltip when button disabled by command?

wpf

提问by Marius

I'm trying to show a tooltip regardless of a buttons state, but this does not seem to do the trick:

无论按钮状态如何,我都试图显示工具提示,但这似乎不起作用:

<Button Command="{Binding Path=CommandExecuteAction}" 
        ToolTip="{Binding Path=Description}" ToolTipService.ShowOnDisabled="true"
        Style="{StaticResource toolbarButton}">
   <Image Source="{Binding Path=Icon}"></Image>
</Button>

How can i show the tooltip when the button is disabled due to command.CanExecute returning false?

当由于 command.CanExecute 返回 false 而禁用按钮时,如何显示工具提示?

Note:

笔记:

ToolTipService.ShowOnDisabled="true" works like a charm. The reason this didn't work in my example is because the style associated with the button redefines the controltemplate and turned off hit-testing on the button when the button was disabled (IsHitTestVisible=false). Re-enabling hit-testing in the controltemplate made the tooltip appear when the button was disabled.

ToolTipService.ShowOnDisabled="true" 就像一个魅力。这在我的示例中不起作用的原因是,与按钮关联的样式重新定义了控件模板,并在禁用按钮时关闭了按钮上的命中测试 (IsHitTestVisible=false)。在控制模板中重新启用命中测试会使工具提示在按钮被禁用时出现。

回答by Kishore Kumar

ToolTipService.ShowOnDisabled="True"

ToolTipService.ShowOnDisabled="True"

回答by sacha barber

This is a good method to add to your startup code

这是添加到启动代码的好方法

ToolTipService.ShowOnDisabledProperty.OverrideMetadata(
    typeof(Control),
    new FrameworkPropertyMetadata(true));

回答by Blechdose

Make tooltip visible for ALL disabled Buttons and Checkboxes:

使工具提示对所有禁用的按钮和复选框可见:

<Window.Resources>
    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}>
        <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
    </Style>
    <Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}>
        <Setter Property="ToolTipService.ShowOnDisabled" Value="true"/>
    </Style>
</Window.Resources>

The BasedOn=...prevents that you loose any other styles that have been applied to checkbox or button before. If you dont use any other styles for button or checkbox you can remove the BasedOn=..parts

BasedOn=...可以防止您丢失之前应用于复选框或按钮的任何其他样式。如果您不使用任何其他样式的按钮或复选框,您可以删除这些BasedOn=..部分