Michael D.
Michael D.

Reputation: 1319

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?

Upvotes: 4

Views: 3287

Answers (3)

LPL
LPL

Reputation: 17063

You give your Control a Background but no child is using it. Two possible solutions:

  1. TemplateBinding

    <Grid Margin="4,0,4,0" Background="{TemplateBinding Background}">
    
  2. DataTrigger with TargetName

    <ControlTemplate TargetType="{x:Type local:InfoBar}">
        <Grid Name="grid" Margin="4,0,4,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            ...
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsError" Value="True" >
                <Setter TargetName="grid" Property="Background" Value="LightPink" />
            </Trigger>
            <Trigger Property="IsError" Value="False" >
                <Setter TargetName="grid" Property="Background" Value="LightYellow" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    

    In this solution you have to change the order: ControlTemplate.Triggers after Grid declaration.

Upvotes: 2

Rohit Vats
Rohit Vats

Reputation: 81253

Try this (Since IsError is DP in your InfoBar and not property of your ControlTemplate)

<DataTrigger Property="{Binding IsError, RelativeSource={RelativeSource
                                         Mode=TemplatedParent}" Value="True">
      <Setter Property="Background" Value="LightPink" />
</DataTrigger>
<DataTrigger Property="{Binding IsError, RelativeSource={RelativeSource
                                      Mode=TemplatedParent}" Value="False">
      <Setter Property="Background" Value="LightYellow" />
</DataTrigger> 

Upvotes: 0

ZanderAdam
ZanderAdam

Reputation: 434

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.

Upvotes: 3

Related Questions