wpf中基于绑定bool属性的样式对象 [英] Style object based on binding bool attribute in wpf

查看:35
本文介绍了wpf中基于绑定bool属性的样式对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 wpf 中的 xaml 在我的 mvvm 设置中更改我要进入的对象的填充颜色.当绑定到的属性设置为 True 时,我想将填充颜色更改为红色.

How can change the fill color of an object I'm being to in my mvvm setup using xaml in wpf. I want to change the fill color to red when the attribute being bound to is set to True.

该属性称为 IsRound.

The attribute is called IsRound.

如有必要,我会发布代码.我现在不在电脑上.

I'll post code if necessary. I'm not on a pc at the moment.

更新

有人可以举例说明如何使用样式触发器来做到这一点吗?并根据绑定属性 bool 设置值?

Could someone show an example of how to do this using style triggers? And set the value based on the bind property bool?

推荐答案

您需要在绑定上使用 IValueConverter.

You need to use an IValueConverter on the binding.

BackgroundColor="{Binding Path=IsRound, Converter={StaticResource BoolToFillColorConverter}}"

public class BoolToFillColorConverter: IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
      bool b;
      if (bool.TryParse(value, out b))
      {
        if (b) return Red
        else return Blue;
      }
      else
      {
        return SomeDefaultColour;
      }
  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    return null;
  }
}

这篇关于wpf中基于绑定bool属性的样式对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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