绑定WPF中的触发器值动态 [英] Binding the Trigger value dynamic in WPF

查看:427
本文介绍了绑定WPF中的触发器值动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ListView 以及 GridView 以表格格式显示数据:

I'm using ListView along with the GridView for displaying the data in tabular format:

<ListView>
 <ListView.View>
   <GridView>
     <GridViewColumn Header="Name" Width="120" DisplayMemberBinding="{Binding Name}" />
     <GridViewColumn Header="Risk" Width="150" DisplayMemberBinding="{Binding RiskName}" />
   </GridView>
</ListView.View>

我必须改变背景基于 RiskName 的颜色。例如,如果 RiskName 是高,那么背景将是红色,如果 RiskName 是中,那么背景将是黄色,等等。

I have to change the background color based on the RiskName. For example, if RiskName is "High" then the background would be Red, if RiskName is "Medium" then the background would be "Yellow", and so on.

我添加了风格以及触发器,根据值设置背景,

I added the style along with trigger to set background based on the value,

<Trigger Property="Text" Value="High">
  <Setter Property="Background" Value="Red"/>
</Trigger>
<Trigger Property="Text" Value="Medium">
  <Setter Property="Background" Value="Yellow"/>
</Trigger>

它工作正常,但在我的情况下, RiskName 不是常数。该值动态。在WPF中有什么办法可以动态设置触发属性的值,看起来像这样吗?

It works fine, but in my case the text of the RiskName is not constant. The value comes dynamically. In WPF is there any way I can set the Value of trigger property dynamically which look something like this?

<Trigger Property="Text" Value="{Binding RiskName}">
    <Setter Property="Background" Value="{Binding RiskBrush}"/>
</Trigger>

任何建议?如果不是,那么其他的工作是什么?

Any suggestion? If not then what is the other work around?

推荐答案

这样的事情

<converters:MyBrushLookupConverter x:Key="brushLookup" BrushDictionary="{Binding KeyedBrushes}" />

其中MyBrushLookupConverter看起来像

where MyBrushLookupConverter looks like

public class MyBrushLookupConverter : DependencyObject, IValueConverter
{
   // This is a dependency property - dependency property gumf omitted for brevity
   public Dictionary<string, Brush> BrushDictionary {get; set;}

   // Convert method
   public Convert(object value, ...)
   {
      return BrushDictionary[(string)value];
   }
}

这篇关于绑定WPF中的触发器值动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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