切换(工具提示)悬停文本以禁用Silverlight按钮

时间:2013-04-15 17:10:55

标签: silverlight button

我的网格在列中显示一个按钮。默认情况下,Silverlight禁用禁用按钮的工具提示显示。我试图通过在边框控件中放置按钮和工具提示对象来解决这个问题。我希望在禁用按钮时显示工具提示,并在启用时不显示。我试图将工具提示绑定到按钮IsEnabled属性,但不起作用。这是代码:

<Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="Transparent"  >
<ToolTipService.ToolTip  >
  <ToolTip Content="Ticket Required"   Visibility="{Binding IsEnabled,     ElementName=btnEdit, Converter={StaticResource BooleanToInvisibilityConverter}}"   />
</ToolTipService.ToolTip>
<Button x:Name="btnEdit" Content="Add"   Margin="0, 0, 7, 0" Width="35" HorizontalAlignment="Right" Command="{Binding EditCommand}" CommandParameter="{Binding}"  Style="{StaticResource SquareButtonStyle}" Click="EditButtonClick" IsTabStop="True"/>
</Border>

无论按钮是启用还是禁用,工具提示都会显示。我究竟做错了什么。我觉得我的绑定有问题,或者这不是这样做的方法。非常感谢。

2 个答案:

答案 0 :(得分:0)

由于某些原因,元素绑定似乎不起作用。绑定机制找不到您尝试绑定的按钮。如果您绑定到视图模型上的属性,它就可以工作。这是一个有效的例子:

<UserControl x:Class="SilverlightApplication14.MainPage"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:SilverlightApplication14"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="400">

    <Grid x:Name="LayoutRoot"
          Background="White">

        <Grid.Resources>

            <local:ReverseBoolToVisibilityConverter x:Key="ReverseBoolToVisibilityConverter"></local:ReverseBoolToVisibilityConverter>

        </Grid.Resources>

        <Border HorizontalAlignment="Center"
                VerticalAlignment="Center"
                Background="Transparent">

            <Button x:Name="btnEdit"
                    IsEnabled="{Binding IsButtonEnabled}"
                    Content="Add"
                    Margin="0, 0, 7, 0"
                    Width="35"
                    HorizontalAlignment="Right"
                    Command="{Binding EditCommand}"
                    CommandParameter="{Binding}"
                    IsTabStop="True" />

            <ToolTipService.ToolTip>
                <ToolTip Content="Ticket Required"
                         Visibility="{Binding IsButtonEnabled, Converter={StaticResource ReverseBoolToVisibilityConverter}}" />
            </ToolTipService.ToolTip>
        </Border>

        <CheckBox HorizontalAlignment="Center"
                  VerticalAlignment="Center"
                  Margin="0 -50 0 0"
                  IsChecked="{Binding IsButtonEnabled, Mode=TwoWay}" Content="Is Enabled"></CheckBox>

    </Grid>
</UserControl>

代码隐藏包括转换器类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication14
{
    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        private bool _IsButtonEnabled;

        public bool IsButtonEnabled
        {
            get { return _IsButtonEnabled; }
            set
            {
                _IsButtonEnabled = value;
                OnPropertyChanged("IsButtonEnabled");
            }
        }

        public MainPage()
        {
            InitializeComponent();

            IsButtonEnabled = true;

            LayoutRoot.DataContext = this;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class ReverseBoolToVisibilityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && (bool)value)
            {
                return Visibility.Collapsed;
            }

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

答案 1 :(得分:0)

它不工作的原因是每当你使用带按钮的命令时,它的enalble禁用属性是由Command的CanExecute方法驱动的。

另外,因为它的方法你可以直接绑定Command的CanExecute方法。因此,您需要一些解决方法来将CanExecute方法的值获取到属性中,并将该属性绑定到ToolTip的Visibility。