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

dan*_*ant 19 c# wpf binding tooltip ivalueconverter

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

我想向用户提供一个工具提示,解释当前禁用某个项目的原因.

我有一个IValueConverter反转布尔IsEnabled属性绑定.但它似乎不适用于这种情况.该ToolTipIS同时显示,当项目被启用和禁用.

因此可以将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
}
Run Code Online (Sandbox Code Playgroud)

和绑定:

<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>
Run Code Online (Sandbox Code Playgroud)

谢谢大家.

dan*_*ant 22

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"
    >
Run Code Online (Sandbox Code Playgroud)

那你需要

<Window.Resources>
    <Res:FalseToStringConverter x:Key="falseToStringConv" />
    <sys:String x:Key="stringToShowInTooltip">This item is disabled because...</sys:String>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

在我的例子中,它是我感兴趣的标签项.它可以是任何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>
Run Code Online (Sandbox Code Playgroud)

代码后面的转换器(或者你想把它放在哪里).注意,我进入了名为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
}
Run Code Online (Sandbox Code Playgroud)

  • +1 for ToolTipService.ShowOnDisabled ="True" (12认同)

Sam*_*Sam 12

有点过时,但我通过将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>
Run Code Online (Sandbox Code Playgroud)