Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change custom control's background color based on its property

I have simple custom control that shows a message to user (something like browser's Info bar).

I have added a Boolean Dependency Property that indicate an error message. If flag is set the background color of control should be red otherwise yellow.

Here is style for the control(in Themes\Generic.xaml):

<Style TargetType="{x:Type local:InfoBar}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:InfoBar}">
                <ControlTemplate.Triggers>
                    <Trigger Property="IsError" Value="True" >
                             <Setter Property="Background" Value="LightPink" />
                    </Trigger>
                    <Trigger Property="IsError" Value="False" >
                             <Setter Property="Background" Value="LightYellow" />
                    </Trigger>                  
                </ControlTemplate.Triggers>

                <Grid Margin="4,0,4,0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="auto" />
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{TemplateBinding Message}" Padding="5" FontWeight="Normal" TextWrapping="Wrap" Grid.Column="0"/>
                    <Button x:Name="PART_CloseButton" Grid.Column="1" VerticalAlignment="Top"  >
                        <Button.Template>
                            <ControlTemplate>
                                <Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Transparent">
                                    <Image Height="16" Width="16" Source="/QOffice.Common.Controls;component/Images/icons/Close.png"  />
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is The control itself:

[TemplatePart(Name = PART_CloseButton, Type = typeof(ButtonBase))]
public class InfoBar : Control
{

    private const string PART_CloseButton = "PART_CloseButton";

    static InfoBar()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(InfoBar), new FrameworkPropertyMetadata(typeof(InfoBar)));

    }

    #region CloseButton

    private ButtonBase _closeButton;
    /// <summary>
    /// Gets or sets the CloseButton template part.
    /// </summary>
    private ButtonBase CloseButton
    {
        get
        {
            return _closeButton;
        }
        set
        {
            if (_closeButton != null)
            {
                _closeButton.Click -= OnButtonClick;
            }

            _closeButton = value;

            if (_closeButton != null)
            {
                _closeButton.Click += OnButtonClick;
            }
        }
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        this.Visibility = System.Windows.Visibility.Collapsed;
    }


    #endregion 


    public override void OnApplyTemplate()
    {
        CloseButton = GetTemplateChild(PART_CloseButton) as ButtonBase;


    }


    #region DependencyProperty Message of InfoBar

    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("Message", typeof(string), typeof(InfoBar),
                new UIPropertyMetadata());

    #endregion


    #region DependencyProperty IsError of InfoBar

    public bool IsError
    {
        get { return (bool)GetValue(IsErrorProperty); }
        set { SetValue(IsErrorProperty, value); }
    }

    public static readonly DependencyProperty IsErrorProperty =
        DependencyProperty.Register("IsError", typeof(bool), typeof(InfoBar),
                new UIPropertyMetadata());

    #endregion





}

As you can see I have defined a property IsError and a trigger to set the background of the control.

But the background is always transparent. Other than that the control if functional.

What is wrong?

like image 687
Michael D. Avatar asked Oct 25 '25 14:10

Michael D.


1 Answers

It seems that your Custom Control is not setting Background Color properly even if I add Background color manually. I am not sure why this is, hopefully someone can elaborate. I did fix your issue though by changing the color of the Grid in your style using:

<Grid.Style>
    <Style TargetType="{x:Type Grid}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBar}}, Path=IsError}" Value="True">
                <Setter Property="Background" Value="LightPink" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:InfoBar}}, Path=IsError}" Value="False">
                <Setter Property="Background" Value="LightYellow" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Grid.Style>

This triggers the background color of the grid based on the IsError value in your InfoBar control.

like image 196
ZanderAdam Avatar answered Oct 27 '25 04:10

ZanderAdam