【问题标题】:Show WPF tooltip on disabled item only仅在禁用项目上显示 WPF 工具提示
【发布时间】:2011-02-22 20:50:55
【问题描述】:

只是想知道是否可以在禁用的项目上显示 WPFONLY(而不是在启用项目时)。

我想给用户一个工具提示,解释为什么项目当前被禁用。

我有一个 IValueConverter 来反转布尔值 IsEnabled 属性绑定。但在这种情况下似乎不起作用。 ToolTip 会在项目启用和禁用时显示。

那么是否可以将 ToolTip.IsEnabled 属性专门绑定到项目自己的 !IsEnabled

我猜这个问题很简单,但代码示例还是在这里:

public class BoolToOppositeBoolConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

        return !(bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        if (targetType != typeof(bool))
            throw new InvalidOperationException("The target must be a boolean");

        return !(bool)value;
    }

    #endregion
}

还有绑定:

<TabItem Header="Tab 2" Name="tabItem2" ToolTip="Not enabled in this situation." ToolTipService.ShowOnDisabled="True" ToolTipService.IsEnabled="{Binding Path=IsEnabled, ElementName=tabItem2, Converter={StaticResource oppositeConverter}}">
    <Label Content="Item content goes here" />
</TabItem>

谢谢各位。

【问题讨论】:

  • 你确定 ToolTipService.ShowOnDisabled="True" 没有在你的反转“之后”执行吗?似乎只需要启用的绑定。
  • @JustABill:可能是这样,但如果没有 ToolTipService.ShowOnDisabled="True",它就无法工作。也许我需要在代码隐藏中处理它。如果可能,我更愿意将 GUI 内容保留在 XAML 中。
  • 在这种情况下,我建议您绑定到 Tooltip,例如 ToolTip="{Binding ElementName=tabItem2, Path=IsEnabled, Converter={StaticResource newconverter}, ConverterParameter=Actual tooltip text goes here}",其中newconverter 是一种新类型,如果值为 true,则返回参数中的值。我猜你的情况是假的。 (我也是从记忆中输入的,所以如果语法关闭,请原谅我)

标签: c# wpf binding tooltip ivalueconverter


【解决方案1】:

JustABill 的建议奏效了。我还需要将字符串定义为资源以避免引号出现问题。而且你仍然需要设置 ToolTipService.ShowOnDisabled="True"。

因此,这里的工作代码显示了如何在 WPF 中显示工具提示在项目被禁用时。

在顶部容器中,包含系统命名空间(参见下面的 sys)。我还有一个 Resources 命名空间,我称之为“Res”。

    <Window x:Class="MyProjectName.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:Res="clr-namespace:MyProjectName.Resources"
    >

那么你需要

<Window.Resources>
    <Res:FalseToStringConverter x:Key="falseToStringConv" />
    <sys:String x:Key="stringToShowInTooltip">This item is disabled because...</sys:String>
</Window.Resources>

在我的例子中,它是一个我感兴趣的标签项。它可以是任何 UI 元素...

<TabItem Name="tabItem2" ToolTipService.ShowOnDisabled="True" ToolTip="{Binding Path=IsEnabled, ElementName=tabItem2, Converter={StaticResource falseToStringConv}, ConverterParameter={StaticResource stringToShowInTooltip}}">
            <Label Content="A label in the tab" />
</TabItem>

以及代码后面的转换器(或您想要放置的任何位置)。请注意,我进入了一个名为 Resources 的命名空间,该命名空间之前已声明过。

public class FalseToStringConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is bool && parameter is string)
        {
            if ((bool)value == false)
                return parameter.ToString();
            else return null;
        }
        else
            throw new InvalidOperationException("The value must be a boolean and parameter must be a string");
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    #endregion
}

【讨论】:

    【解决方案2】:

    有点过时了,但我通过将 RelativeSource 模式设置为 Self 而不是在 Binding 中设置 ElementName 来实现这一点。

    <TabItem Header="Tab 2" Name="tabItem2" ToolTip="Not enabled in this situation." ToolTipService.ShowOnDisabled="True" ToolTipService.IsEnabled="{Binding Path=IsEnabled, RelativeSource={RelativeSource Mode=Self}, Converter={StaticResource oppositeConverter}}">
        <Label Content="Item content goes here" />
    </TabItem>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多