Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
    <SolidColorBrush Color="{StaticResource PhoneAccentColor}"/>
</Rectangle.Fill>
like image 481
Spruce Avatar asked Mar 07 '11 12:03

Spruce


1 Answers

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.

like image 98
AnthonyWJones Avatar answered Oct 16 '22 12:10

AnthonyWJones