Daniel
Daniel

Reputation: 411

Changed Event when Background Dependency Property of Custom control changes

I'm creating custom WPF controls with specific properties (and events). My Button should be able to change its Foreground color according to the currently set background Color of the Button.

Now, this works when changing the color in the overridden OnApplyTemplate function, but I haven't found out how to do this dynamically (after the control is loaded).

If I could add a DependencyPropertyChanged event handler to a Controls Background property this would solve my problems, but I don't see how.

Other, rather ugly solution which would work:

Has anybody got a solution?

EDIT:

Ok, after seeing that the Background property of a control may change in accordance with its parents Background property (if not specifically set) I will add a separate BackgroundColor property which really only affects the background of the Button.

Upvotes: 4

Views: 2357

Answers (1)

Sascha Hennig
Sascha Hennig

Reputation: 2572

You could possibly do this with binding converters.

http://tech.pro/tutorial/806/wpf-tutorial-binding-converters

Or, maybe easier to implement, triggers.

http://wpftutorial.net/Triggers.html

Edit (some examples):

Binding Converter Sample - on a UserControl only, but should show how it could be done.

in UCButton.xaml.cs:

using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace Test3
{
    public partial class UCButton : UserControl
    {
        public UCButton()
        {
            InitializeComponent();

            this.DataContext = this;
        }
    }

    [ValueConversion(typeof(Brush), typeof(Brush))]
    public class BrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            Brush background = (Brush)value;

            if (background == Brushes.Pink)
                return Brushes.Red;
            else if (background == Brushes.LightBlue)
                return Brushes.DarkBlue;
            else if (background == Brushes.LightGreen)
                return Brushes.DarkGreen;
            else
                return Brushes.Black;
        }

        public object ConvertBack(object value, Type targetType,
               object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

UCButton.xaml

<UserControl x:Class="Test3.UCButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Test3" mc:Ignorable="d" 
             d:DesignHeight="29" d:DesignWidth="82">

    <UserControl.Resources>
        <local:BrushConverter x:Key="brushConverter" />
    </UserControl.Resources>

    <Grid>
        <Button Content="Button" Name="button1" Background="{Binding Background}" Foreground="{Binding Background, Converter={StaticResource brushConverter}}" />
    </Grid>
</UserControl>

Example for using Triggers:

in MainWindow.xaml add:

<Window.Resources>
    <Style x:Key="buttonBrushStyle" TargetType="Button">
        <Style.Triggers>
            <Trigger Property="Background" Value="Pink">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
            <Trigger Property="Background" Value="LightBlue">
                <Setter Property="Foreground" Value="DarkBlue" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Button Style="{StaticResource buttonBrushStyle}" Content="Button"
   Height="23" Width="75" Background="Pink" />
<Button Style="{StaticResource buttonBrushStyle}" Content="Button"
   Height="23" Width="75" Background="LightBlue" />

Upvotes: 3

Related Questions