Blog

I want to bind the background color of a button to an enum. I wonder if there is an enum object that can hold multiple values, for example the state and the color. I want to avoid two enums that could be out of sync. Here are two enums that I would like to integrate with each other.

enum StateValue { Player, Wall, Box }
enum StateColor { Colors.Red, Colors.Grey, Colors.Brown }

Then I need to create a binding for the XAML button.

<Button Content="Player" Background="{Binding Source=...?}" />

Maybe, a dictionary like the following is helpful. But still I do not know how the binding needs to be written.

public Dictionary<StateValue, Color> stateValueColor = 
new Dictionary<ElementState, Color>()
{
 { StateValue.Player, Colors.Red },
 { StateValue.Wall, Colors.Grey },
 { StateValue.Box, Colors.Brown }
};

By default, the Validation.ErrorTemplate in WPF is just a small red border without any ToolTip.

In Silverlight 4, the validation error is nicely styled out-of-the-box.

Here is a comparison of a validation error occuring in Silverlight 4 and WPF

Silverlight 4

WPF

Notice the really flat, boring look of the WPF version compared to the, in my opinion, great look in Silverlight.

Does any similar validation styles/templates exist in the WPF Framework or has anybody created nicely styled validation templates like the Silverlight version above? Or will I have to create them from scratch?

If anybody wants to try it out, the validation error above can be reproduced with the following code, works for both Silverlight and WPF

MainWindow/MainPage.xaml

<StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Top">
    <TextBox Text="{Binding Path=TextProperty, 
           Mode=TwoWay, ValidatesOnExceptions=True}"/>
    <Button Content="Tab To Me..." Margin="20,0,0,0"/>
</StackPanel>

MainWindow/MainPage.xaml.cs

public MainWindow/MainPage()
{
    InitializeComponent();
    this.DataContext = this;
}

private string _textProperty;
public string TextProperty
{
    get { return _textProperty; }
    set
    {
        if (value.Length > 5)
        {
            throw new Exception("Too many characters");
        }
        _textProperty = value;
    }
}

I want all characters in a TextBlock to be displayed in uppercase

<TextBlock Name="tbAbc"
       FontSize="12"
       TextAlignment="Center"
       Text="Channel Name"
       Foreground="{DynamicResource {x:Static
          r:RibbonSkinResources.RibbonGroupLabelFontColorBrushKey}}" />

The strings are taken through Binding. I don't want to make the strings uppercase in the dictionary itself.