19

只是想知道是否可以在禁用的项目上显示 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>

谢谢各位。

4

2 回答 2

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"
    >

那你需要

<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
}
于 2010-05-24T22:11:56.300 回答
13

有点过时了,但我通过将 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>
于 2014-07-14T10:47:19.793 回答