我正在开发 WPF 应用程序。我已将文本块绑定到按钮。当关联按钮的 isEnabled 为 true 时,我想将文本块的前景设置为黑色。
我想使用转换器来做到这一点。但它不起作用。也没有给出任何错误。
我已在“模型”文件夹中声明了以下课程。
public class BrushColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
{
return System.Windows.Media.Colors.Black;
}
}
return System.Windows.Media.Colors.LightGreen;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
按钮的enable、isable属性从viewmodel改变(例如使用 RaiseCanExecuteChanged)())
XAML 中与文本块相关的内容有:
<Window.Resources>
<local:BrushColorConverter x:Key="BConverter"></local:BrushColorConverter>
</Window.Resources>
<Button>(!..all button properties..!)</Button>
<TextBlock x:Name="AnswerText"
Text="Answer"
Foreground="{Binding ElementName=AnswerButton,Path=IsEnabled, Converter={StaticResource BConverter}}"
TextWrapping="Wrap"/>
使用
返回新的 SolidColorBrush(Colors.Black);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)