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

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

问题描述

我正在处理 WPF 应用程序.我已将我的文本块绑定到我的按钮.当其关联按钮的 isEnabled 为 true 时,我想将我的文本块的前景设置为黑色.我想使用转换器来做到这一点.但它不起作用.也没有给出任何错误.我在模型"文件夹中声明了以下类.

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

按钮的启用、禁用属性更改从视图模型(例如使用 RaiseCanExecuteChanged)())

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"/>

推荐答案

use返回新的 SolidColorBrush(Colors.Black);

use return new SolidColorBrush(Colors.Black);

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

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