【问题标题】:WPF Tooltip VisibilityWPF 工具提示可见性
【发布时间】:2011-03-10 02:33:09
【问题描述】:

如何确保按钮的 Tooltip 仅在按钮被禁用时可见?

我可以将工具提示的可见性绑定到什么?

【问题讨论】:

  • 什么时候禁用? - 你的意思是“没有”被禁用吗?
  • 显示一个工具提示来描述为什么你不能触摸这个按钮可能是有意义的。如果这是大卫的意图,我认为这很有意义。
  • 是的,我想是的,我没有挑剔。我只是真的很感兴趣:)
  • 我的意思是禁用了。正如 reuscam 建议的那样,工具提示是解释为什么按钮被禁用。

标签: wpf binding tooltip visibility


【解决方案1】:

您需要在按钮上将ToolTipService.ShowOnDisabled 设置为 True,以便在禁用按钮时让工具提示完全可见。您可以在 Button 上绑定 ToolTipService.IsEnabled 以启用和禁用 Tooltip。

【讨论】:

  • 对于任何想和我做同样事情的人,我已经发布了按钮的完整 xaml 作为答案。感谢您的帮助。
【解决方案2】:

这是 Button 的完整 XAML(基于 @Quartermeister 的回答)

<Button 
  x:Name="btnAdd" 
  Content="Add" 
  ToolTipService.ShowOnDisabled="True" 
  ToolTipService.IsEnabled="{Binding ElementName=btnAdd, Path=IsEnabled, Converter={StaticResource boolToOppositeBoolConverter}}" 
  ToolTip="Appointments cannot be added whilst the event has outstanding changes."/>

【讨论】:

    【解决方案3】:

    您也可以使用简单的触发器来完成。只需将以下代码放入 Window 中即可。

    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <CheckBox Name="chkDisabler" Content="Enable / disable button" Margin="10" />
        <Button Content="Hit me" Width="200" Height="100" IsEnabled="{Binding ElementName=chkDisabler, Path=IsChecked}">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="ToolTipService.ShowOnDisabled" Value="true" />
                    <Setter Property="ToolTip" Value="{x:Null}" />
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="ToolTip" Value="Hi, there! I'm disabled!" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
    </StackPanel>
    

    【讨论】:

    • 这很方便查看通过Style setter 设置ShowOnDisabled 的语法。
    • 如果我正确解释了这段代码,可以设置两个触发器来提供两个不同的工具提示,一个用于禁用按钮,一个用于启用?
    • 是的 Mike,您可以为 IsEnabled 添加第二个触发器,并为此添加 True。
    【解决方案4】:

    对 David Ward 提出的建议稍作修改。这是完整的代码

    向这样的资源添加值转换器

    <Window.Resources>
        <Converters:NegateConverter x:Key="negateConverter"/>
    </Window.Resources>
    

    然后定义下面的xaml

    <Button 
      x:Name="btnAdd" 
      Content="Add" 
      ToolTipService.ShowOnDisabled="True" 
      ToolTipService.IsEnabled="{Binding RelativeSource={RelativeSource self}, Path=IsEnabled, Converter={StaticResource negateConverter}}" 
      ToolTip="Hi guys this is the tool tip"/>
    

    值转换器如下所示

    [ValueConversion(typeof(bool), typeof(bool))]
      public class NegateConverter : IValueConverter
      {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
         return  !((bool)value);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
          throw new NotImplementedException();
        }
      }
    

    【讨论】:

      猜你喜欢
      • 2021-04-01
      • 2011-03-31
      • 1970-01-01
      • 2012-01-16
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      相关资源
      最近更新 更多