简体   繁体   中英

WPF change background of a control with an attached property

i need to change the background of labels and buttons when a boolean variable is true (back to default color on false). so i wrote an attached property. it looks like this so far:

public class BackgroundChanger : DependencyObject
{
    #region dependency properties
    // status
    public static bool GetStatus(DependencyObject obj)
    {
        return (bool)obj.GetValue(StatusProperty);
    }
    public static void SetStatus(DependencyObject obj, bool value)
    {
        obj.SetValue(StatusProperty, value);
    }
    public static readonly DependencyProperty StatusProperty = DependencyProperty.RegisterAttached("Status",
                     typeof(bool), typeof(BackgroundChanger), new UIPropertyMetadata(false, OnStatusChange));

    #endregion

    private static void OnStatusChange(DependencyObject obj,  DependencyPropertyChangedEventArgs e) 
    {
        var element = obj as Control;
        if (element != null)
        {
            if ((bool)e.NewValue)
                element.Background = Brushes.LimeGreen;
            else
                element.Background = default(Brush);
        }
    }
}

and i use it like this:

<Label CustomControls:BackgroundChanger.Status="{Binding test}" />

it works fine. when the according variable test is set in the viewmodel, the backgroundcolor changes to LimeGreen .

my question:

the color LimeGreen is hard coded. i would like to set that color (and the default color as well) in the XAML as well. so i can decide between which two colors the background switches. how can i do that?

You can have multiple attached properties. Accessing them is easy, there are static Get.. and Set.. methods, to which you supply DependencyObject which attached property value you want to operate.

public class BackgroundChanger : DependencyObject
{
    // make property Brush Background
    // type "propdp", press "tab" and complete filling

    private static void OnStatusChange(DependencyObject obj,  DependencyPropertyChangedEventArgs e) 
    {
        var element = obj as Control;
        if (element != null)
        {
            if ((bool)e.NewValue)
                element.Background = GetBrush(obj); // getting another attached property value
            else
                element.Background = default(Brush);
        }
    }
}

In xaml it will look like

<Label CustomControls:BackgroundChanger.Status="{Binding test}"
    CustomControls:BackgroundChanger.Background="Red"/>

Why not use a DataTrigger instead?

<Style x:Key="FilePathStyle" TargetType="TextBox">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ElementName=AddButton, Path=IsEnabled}" Value="True">
            <Setter Property="Background" Value="#FFEBF7E1" />
        </DataTrigger>

        <DataTrigger Binding="{Binding ElementName=AddButton, Path=IsEnabled}" Value="False">
            <Setter Property="Background" Value="LightYellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>

.
.
.
<TextBox Style="{StaticResource FilePathStyle}" x:Name="filePathControl" Width="300" Height="25" Margin="5" Text="{Binding SelectedFilePath}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM