您可以使用值转换器将 TextBox 的文本转换为按钮的 Opacity 值:
<TextBox x:Name="txtBox" />
<Button Opacity="{Binding Text, ElementName=txtBox, Converter={StaticResource textToOpacityConverter}}" Content="Go !" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="283,-3,0,0" Width="161" Name="searchbutton" Click="search" Height="78" BorderBrush="Transparent" />
这是值转换器:
public class TextToOpacityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (string.IsNullOrEmpty(value as string))
{
return 0.5;
}
return 1;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
如果您需要有关 ValueConverter 的更多信息,可以在此处查看http://www.c-sharpcorner.com/uploadfile/dpatra/value-converter-in-wpf-part-i/