Silverlight 4工具提示可见性绑定

时间:2011-12-09 17:33:28

标签: binding tooltip visibility

我正在尝试在XAML中绑定工具提示可见性,我遇到了一个令人困惑的问题,我的可见性绑定在其他控件上工作正常但在工具提示上没有。

我有一个带有提交按钮的表单,在未输入所需字段时将其禁用。当按钮被禁用时,我想要一个带有相关消息的工具提示。启用后,不需要工具提示。为了实现这一点,我将按钮放在透明边框中并将工具提示设置在边框上,因为按钮本身的工具提示在禁用时永远不会显示。但是,绑定到工具提示的可见性失败,似乎我只能更改代码隐藏中的可见性。我可以对各种控件的可见性使用完全相同的绑定(在下面的示例中,我也在TextBlock上使用它)。如果我在代码隐藏中应用完全相同的绑定,它可以正常工作。为什么这不适用于XAML?

XAML

<UserControl.Resources>
    <local:VisibilityConverter x:Key="visibilityConverter"/>
    <local:VisibilityConverter x:Key="reversedVisibilityConverter" IsReversed="True"/>
</UserControl.Resources>

<StackPanel Background="White"
            Width="310">
    <TextBlock Text="Using XAML binding for tooltip visibility..."
               FontWeight="Bold"/>
    <CheckBox x:Name="cbEnable"
              Content="Enable Submit Button"/>
    <Border Background="Transparent"
            Margin="0,10,0,0">
        <ToolTipService.ToolTip>
            <!-- This has the same binding as the 2nd TextBlock below, it should be visible when cbEnable is NOT checked and collapsed when it is checked -->
            <ToolTip Content="Submit Button Is Disabled"
                     Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource reversedVisibilityConverter}}"/>
        </ToolTipService.ToolTip>

        <Button Content="Submit"
                IsEnabled="{Binding IsChecked, ElementName=cbEnable}"/>
    </Border>

    <!-- This TextBlock is visibile when cbEnable is checked -->
    <TextBlock Text="Submit Button is enabled"
               Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource visibilityConverter}}"/>
    <!-- This TextBlock is visibile when cbEnable is NOT checked (same as ToolTip binding above -->
    <TextBlock Text="Submit Button is disabled"
               Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource reversedVisibilityConverter}}"/>

    <TextBlock Text="Using code-behind binding for tooltip visibility..."
               FontWeight="Bold"
               Margin="0,20,0,0"/>
    <CheckBox x:Name="cbEnable2"
              Content="Enable Submit Button"/>

    <Border Background="Transparent"
            Margin="0,10,0,0">
        <ToolTipService.ToolTip>
            <ToolTip x:Name="toolTip2"
                     Content="Submit Button 2 Is Disabled"/>
        </ToolTipService.ToolTip>

        <Button Content="Submit 2"
                IsEnabled="{Binding IsChecked, ElementName=cbEnable2}"/>
    </Border>

    <TextBlock Text="Submit Button 2 is enabled"
               Visibility="{Binding IsChecked, ElementName=cbEnable2, Converter={StaticResource visibilityConverter}}"/>
    <TextBlock Text="Submit Button 2 is disabled"
               Visibility="{Binding IsChecked, ElementName=cbEnable2, Converter={StaticResource reversedVisibilityConverter}}"/>
</StackPanel>

代码隐藏:

public partial class MainPage : UserControl {
    public MainPage() {
        InitializeComponent();

        toolTip2.SetBinding(ToolTip.VisibilityProperty, new System.Windows.Data.Binding("IsChecked") {
            Source = cbEnable2,
            Converter = new VisibilityConverter() { IsReversed = true }
        });
    }
}

VisibilityConverter:

public class VisibilityConverter : IValueConverter {
    public bool IsReversed { get; set; }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        bool isVisible = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
        if (IsReversed) {
            isVisible = !isVisible;
        }
        return (isVisible ? Visibility.Visible : Visibility.Collapsed);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        bool isVisible = ((Visibility)value == Visibility.Visible);
        if (IsReversed) {
            isVisible = !isVisible;
        }
        return isVisible;
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,我不知道为什么要修复它,但这就是我所做的:我在ViewModel中重命名了该属性。是的,我知道。似乎很荒谬。我将属性从IsWaitingVisible更改为WaitingVisibility。我有了这个主意,因为我将ViewModel属性从bool更改为text,然后将其绑定到临时的可见TextBlock。它不会以相同的方式显示同一ViewModel中的不同属性的值。那真是太疯狂所以我把这个地方改名为别的东西瞧!文本开始出现在UI中。然后,我重新连接了网格的visibility属性(并将我的VisibilityConverter更改为使用字符串而不是bool),一切正常。

我想为了科学的目的,我应该将属性名称更改回IsWaitingVisible并查看它是否中断。如果是这样,我将不得不得出结论,这是SL 5中的一个难题。

当我考虑在Silverlight中构建可靠的应用程序时,这种诡异让我感到害怕。