WPF绑定到备用静态资源 [英] WPF Binding to alternate static resource

查看:179
本文介绍了WPF绑定到备用静态资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据项目的状态将静态资源绑定到视图模型项目.视图中有一个项目列表.

I am attempting to bind a static resource to a view model item based on the state of the item. There is in a list of items in the view.

ViewModel具有布尔属性State. 视图具有绑定到ObservableCollection的ItemsControl

The ViewModel has a boolean property State. The View has a ItemsControl bound to an ObservableCollection

我为所需的每个项目定义了两个资源字符串.
如何根据State的值显示正确的字符串?

I have two resource strings defined for each of the items required.
How do I display the correct string based on the value of State?

谢谢, 埃里克

推荐答案

在转换器中使用绑定.

这里是一个例子:

<config:BoolToBrushConverter x:Key="Bool2Brush"
                      TrueBrush="{StaticResource OKStatusBrush}"
                      FalseBrush="{StaticResource ErrorStatusBrush}" />

<TextBlock Name="tbx1" Grid.Row="2" Grid.Column="1" 
    Background="{Binding Path=State, Converter={StaticResource Bool2Brush}}"...

看看如何使您的自定义转换器能够接受资源?只需在资源中简单定义转换器,然后引用您需要的两个即可.然后让Binding知道您的转换器.

See how you can enable your custom converter to accept resources? Simply define converter in resources too and reference the two you need. Then let the Binding know about your converter.

这篇关于WPF绑定到备用静态资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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