0

我的网格在一列中显示一个按钮。默认情况下,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>

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

4

2 回答 2

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
    }
}
于 2013-04-16T09:50:45.663 回答
0

它不起作用的原因是每当您使用带有按钮的命令时,它的启用禁用属性都是由命令的 CanExecute 方法驱动的。

也因为它的方法你不能直接绑定 Command 的 CanExecute 方法。因此,您需要一些解决方法将 CanExecute 方法的值获取到属性中并将该属性绑定到 ToolTip 的可见性。

于 2014-07-18T14:51:26.390 回答