WPF, Binding, and Background colors.

So for my ToDo applications, one of the first task items was being able to change the font color.  After reading about this on my trusty WPF advisor blog, I learned you can do direct data bindings with Settings.  This is useful since I just have to create the property bind to it, and walk away.  The one thing I have to do is just save the properties.

Here is the WPF for doing the background binding.  Took me a bit to see the error of my ways for binding a background.  I had <Button Background=“{Binding …” and this was a no no even though you can say Background=“Red” and have it be red.  By binding like this, it works great.

I also bolded the important items to have if you want to bind to your settings.

<Window x:Class="ToDo.Configuration"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Properties="clr-namespace:ToDo.Properties"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Configuration" Height="300" Width="300">
    <Window.Resources>
        <Properties:Settings x:Key="settings" />
    </Window.Resources>
    <Grid DataContext="{StaticResource settings}">
        <Button>
            <Button.Background>
                <SolidColorBrush Color="{Binding Path=Default.FontColor, Mode=TwoWay}" />
            </Button.Background>
        </Button>
    </Grid>
</Window>

Now I can have multiple things bound to a setting and not have to reload after the settings have been updated!

Ishmir Nov 23, 2009 @ 10:17 AM

# re: WPF, Binding, and Background colors.
You can bind the background of a button with the <Button Background=“{Binding …” syntax. Just make sure the property you bind to is a string (containing a color name or a hex value).

Post a Comment

Please add 3 and 2 and type the answer here: