Skip to content

Instantly share code, notes, and snippets.

@jhalbrecht
Last active December 17, 2015 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhalbrecht/5653797 to your computer and use it in GitHub Desktop.
Save jhalbrecht/5653797 to your computer and use it in GitHub Desktop.
This ValueConverter expects a string of either "Heads" or "Tails" and returns Green or Red respectively or yellow as a default or unknown value. I use it in a Windows Store Application, "Decisive" to color the text result of a coin toss. I've been using a long weekend as a sprint to focus on learning VS2012 and blend design tools. Blend made it …
using System;
using Windows.UI;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;
namespace Decisive.Common
{
public sealed class HeadsOrTailsToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
switch (value as string)
{
case "Heads":
return new SolidColorBrush(Colors.Green);
case "Tails":
return new SolidColorBrush(Colors.Red);
default:
return new SolidColorBrush(Colors.Yellow);
}
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
<Page.Resources>
<uncommon:HeadsOrTailsToColorConverter x:Key="TextToColor" />
</Page.Resources>
<TextBlock x:Name="textBlockYeaNoAnswer"
Grid.Column="0"
Grid.Row="1"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="{Binding YeaNoAnswer}"
FontSize="56"
Foreground="{Binding Text, Converter={StaticResource TextToColor}, RelativeSource={RelativeSource Mode=Self}}"
FontWeight="Bold" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment