开发者

Converter for Static Resources on Windows Phone

Hopefully this should be an easy one, i have a background of a rectangle i want to display as the phone accent colour or a disabled color based on a boolean in my view model.

I assume that converters are the way to go, but not sure of the syntax to get access to the static resources.

<Rectangle.Fill>
    <So开发者_开发知识库lidColorBrush Color="{StaticResource PhoneAccentColor}"/>
</Rectangle.Fill>


Grab the code for a generic BoolToValueConverter from this blog article:-

A Generic Boolean Value Converter

Also include in your code this specialisation for a converter to a Brush:-

public class BoolToBrushConverter : BoolToValueConverter<Brush> { }

Now add the converter to your Xaml like this:-

<Grid.Resources>
   <local:BoolToBrushConverter x:Key="DisabledBrushConv"
      FalseValue="{StaticResource PhoneAccentBrush}"
      TrueValue="{StaticResource PhoneDisabledBrush}" />
</Grid>

Then in rectangle :-

 <Rectangle Fill="{Binding Disabled, Converter={StaticResource DisabledBrushConv}}" ... />

This assumes the property in your view model is called Disabled.


You have two options:

  1. Use a converter
  2. Define a property on your viewmodel that returns a Brush based on the boolean value. I would prefer this solution because the performance hit of converters are more noticable on the phone than on the desktop.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜