在禁用的项目上显示WPF工具提示

22

想知道是否有可能仅在禁用的项目上显示 WPF,而不是在启用项目上显示。

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

我有一个 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
@JustABill:这可能是情况,但如果没有 ToolTipService.ShowOnDisabled="True",它就无法工作。也许我需要在代码后台中处理它。如果可能的话,我更喜欢将 GUI 方面的东西保留在 XAML 中。 - dant
在这种情况下,我建议您绑定到工具提示,例如ToolTip="{Binding ElementName=tabItem2, Path=IsEnabled, Converter={StaticResource newconverter}, ConverterParameter=实际的工具提示文本}", 其中newconverter是一个新类型,如果值为true,则返回参数中的值。在你的情况下应该是false。(我是凭记忆打的,如果语法有误请见谅) - JustABill
2个回答

23

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

所以这里是工作代码,展示了如何在WPF中在项目被禁用时显示提示。

在顶级容器中,包含系统命名空间(请参见下面的sys)。我还有一个资源命名空间,我称之为"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>

还有一个转换器在代码后端(或您想放置的任何位置)。请注意,我的转换器位于一个名为资源的命名空间中,该命名空间早先被声明过。

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
}

18

有一点过时了,但我通过将RelativeSource模式设置为Self而不是在绑定中设置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>

谢谢,这个看起来比被接受的解决方案简单多了! - Frederik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接