【发布时间】: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