Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Separate backgroundworker for each Button checking the current Background color (could be a real performance killer if enough buttons are present)
  • Use a custom BackgroundColor property instead of Background (would work, but does not seem very elegant)

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.

like image 707
Daniel Avatar asked Sep 15 '25 03:09

Daniel


1 Answers

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" />
like image 93
Sascha Hennig Avatar answered Sep 16 '25 16:09

Sascha Hennig