【问题标题】:use of boolean to color converter in XAML在 XAML 中使用布尔到颜色转换器
【发布时间】:2012-01-21 22:38:12
【问题描述】:

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

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

【问题讨论】:

  • 尝试返回字符串:System.Windows.Media.Colors.Black.ToString()

标签: c# wpf xaml converter


【解决方案1】:

使用 返回新的 SolidColorBrush(Colors.Black);

【讨论】:

  • 谢谢。那更准确,因为我可以直接返回颜色而不是返回“字符串”。
【解决方案2】:

上面的答案向您展示了如何正确使用转换器。但是,您真的需要使用转换器吗?这只能在 XAML 中使用 Triggers 来完成:

XAML

        <StackPanel>

            <Button IsEnabled="{Binding ElementName=isEnabledCheckBox, Path=IsChecked}">
                <TextBlock Text="Answer" TextWrapping="Wrap">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Button}}, Path=IsEnabled}" Value="True">
                                    <Setter Property="Foreground" Value="Green"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Button>

            <Button IsEnabled="{Binding ElementName=isEnabledCheckBox, Path=IsChecked}">
                <TextBlock Text="Answer" TextWrapping="Wrap">
                    <TextBlock.Style>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <Trigger Property="IsEnabled" Value="True">
                                    <Setter Property="Foreground" Value="Green"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </Button>

            <CheckBox x:Name="isEnabledCheckBox" Content="Toggle IsEnable on Buttons above" />

        </StackPanel>

在上面的示例中,第一个TextBlock 使用DataTrigger 绑定到其父级的IsEnabled 属性,如果为真,则将Foreground 设置为某种颜色。

但是,这太过分了 - WPF 会自动将IsEnabled 属性向下传播给子级。也就是说,如果您在Button 上将IsEnabled 设置为false,那么您的TextBlock 将自动将其IsEnabled 属性更新为false。这在第二个TextBlock 中得到了证明,它使用属性Trigger 来检查它自己的IsEnabled 属性与true 的值(因为它的IsEnabled 属性将与其父属性相同)。这将是首选方法。

希望这会有所帮助!

【讨论】:

  • 这也是一部很棒的作品。我之前也尝试过,但我无法绑定到它的父母。但是 relativesource 会解决这个问题。谢谢。
  • 如果你使用Material Design in XAML之类的,这将不起作用,因为设置样式触发器会覆盖控件的整个材料设计样式。
【解决方案3】:

为了使这个转换器通用,您可以使用ConverterParameter 来指定当value 为真或假时要插入的颜色。不透明度也可能是有趣的。我在这里提供了我采用参数 [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber] 的转换器。

由于@user1101511 提到的SolidColorBrush() 方法是System.Windows.Media 库的一部分,它使用来自同一库的Color 类型。这种类型没有Color.FromName() 方法,如System.Drawing.Color 类。

因此,我创建了一个名为 ColorFromName(string name) 的辅助方法。如果ConverterParameter 的交互失败,我将"LimeGreen" 指定为备用颜色。就我而言,当value 为假时,我希望输出为"Transparent"

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace MyConverters
{
    [ValueConversion(typeof(bool), typeof(SolidColorBrush))]
    class BoolToColorBrushConverter : IValueConverter
    {
        #region Implementation of IValueConverter

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value">Bolean value controlling wether to apply color change</param>
        /// <param name="targetType"></param>
        /// <param name="parameter">A CSV string on the format [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber] may be provided for customization, default is [LimeGreen;Transperent;1.0].</param>
        /// <param name="culture"></param>
        /// <returns>A SolidColorBrush in the supplied or default colors depending on the state of value.</returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        SolidColorBrush color;
        // Setting default values
        var colorIfTrue = Colors.LimeGreen;
        var colorIfFalse = Colors.Transparent;
        double opacity = 1;
        // Parsing converter parameter
        if (parameter != null)
        {
            // Parameter format: [ColorNameIfTrue;ColorNameIfFalse;OpacityNumber]
            var parameterstring = parameter.ToString();
            if (!string.IsNullOrEmpty(parameterstring))
            {
                var parameters = parameterstring.Split(';');
                var count = parameters.Length;
                if (count > 0 && !string.IsNullOrEmpty(parameters[0]))
                {
                    colorIfTrue = ColorFromName(parameters[0]);
                }
                if (count > 1 && !string.IsNullOrEmpty(parameters[1]))
                {
                    colorIfFalse = ColorFromName(parameters[1]);
                }
                if (count > 2 && !string.IsNullOrEmpty(parameters[2]))
                {
                    double dblTemp;
                    if (double.TryParse(parameters[2], NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture.NumberFormat, out dblTemp))
                        opacity = dblTemp;
                }
            }
        }
        // Creating Color Brush
        if ((bool) value)
        {
            color = new SolidColorBrush(colorIfTrue);
            color.Opacity = opacity;
        }
        else
        {
            color = new SolidColorBrush(colorIfFalse);
            color.Opacity = opacity;
        }
        return color;
    }


    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion

    public static Color ColorFromName(string colorName)
    {
        System.Drawing.Color systemColor = System.Drawing.Color.FromName(colorName);
        return Color.FromArgb(systemColor.A, systemColor.R, systemColor.G, systemColor.B);
    }
}

从 xaml 可以像这样使用上述转换器:

Background="{Binding MyBooleanValue, Converter={StaticResource BoolToColorBrushConverter}, ConverterParameter=LimeGreen;Transperent;0.2, Mode=OneWay}"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-19
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多