由于某种原因,元素绑定似乎在这里不起作用。绑定机制找不到您尝试绑定的按钮。如果您改为绑定到视图模型上的属性,它就会起作用。这是一个工作示例:
<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
}
}