WPF Binding to alternate static resource

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 12:13:07

问题


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.

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

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?

Thanks, Eric


回答1:


To use the static resource you can use ObjectDataProvider. And to bind it to your View based on the boolean value, you can use proper Converter




回答2:


Use Binding with a Converter.

Here is an example:

<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}}"...

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.



来源:https://stackoverflow.com/questions/20408502/wpf-binding-to-alternate-static-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!