使用布尔以颜色XAML转换器 [英] use of boolean to color converter in XAML

查看:227
本文介绍了使用布尔以颜色XAML转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF application.I工作已绑定我的文字块到我的按钮。我想我的文字块的前景色设置为黑色的颜色,当其相关按钮的IsEnabled是真实的。
我想做到这一点使用转换器。 但它不工作。也没有给予任何错误。
我已经声明以下类在我的模型文件夹

I am working on WPF application.I have bound my textblock to my button. I want to set foreground of my textblock to black color when its associated button's isEnabled is true. I want to do this using converter. But its not working. also not giving any error. I have declared following class in my "Models" folder.

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();
    }

}

按钮的启用,isable

Button's enable,isable property changes from viewmodel(e.g using RaiseCanExecuteChanged)())

在XAML文本块有关的事情都是属性更改

textblock related things in XAML are :

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

use return new SolidColorBrush(Colors.Black);

这篇关于使用布尔以颜色XAML转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆