2013-04-15 51 views
0

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

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

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

回答

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 
    } 
} 
0

它不工作的原因是当你使用命令按钮的enalble禁用特性是驱动程序的命令的CanExecute方法。

也因为它的方法你不能直接绑定Command的CanExecute方法。因此,您需要一些解决方法来将CanExecute方法的值传递给属性,并将该属性绑定到ToolTip的Visibility。