title: How to change custom control's background color based on its property
body: <p>I have simple custom control that shows a message to user (something like browser's Info bar).</p>
  
  <p>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.</p>
  
  <p>Here is style for the control(in Themes\Generic.xaml):</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:InfoBar}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:InfoBar}"&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsError" Value="True" &gt;
                               &lt;Setter Property="Background" Value="LightPink" /&gt;
                      &lt;/Trigger&gt;
                      &lt;Trigger Property="IsError" Value="False" &gt;
                               &lt;Setter Property="Background" Value="LightYellow" /&gt;
                      &lt;/Trigger&gt;                  
                  &lt;/ControlTemplate.Triggers&gt;
  
                  &lt;Grid Margin="4,0,4,0"&gt;
                      &lt;Grid.ColumnDefinitions&gt;
                          &lt;ColumnDefinition Width="*" /&gt;
                          &lt;ColumnDefinition Width="auto" /&gt;
                      &lt;/Grid.ColumnDefinitions&gt;
                      &lt;TextBlock Text="{TemplateBinding Message}" Padding="5" FontWeight="Normal" TextWrapping="Wrap" Grid.Column="0"/&gt;
                      &lt;Button x:Name="PART_CloseButton" Grid.Column="1" VerticalAlignment="Top"  &gt;
                          &lt;Button.Template&gt;
                              &lt;ControlTemplate&gt;
                                  &lt;Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Transparent"&gt;
                                      &lt;Image Height="16" Width="16" Source="/QOffice.Common.Controls;component/Images/icons/Close.png"  /&gt;
                                  &lt;/Border&gt;
                              &lt;/ControlTemplate&gt;
                          &lt;/Button.Template&gt;
                      &lt;/Button&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Here is The control itself:</p>
  
  <pre><code>[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;
      /// &lt;summary&gt;
      /// Gets or sets the CloseButton template part.
      /// &lt;/summary&gt;
      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
  
  
  
  
  
  }
  </code></pre>
  
  <p>As you can see I have defined a property IsError and a trigger to set the background of the control. </p>
  
  <p>But the background is always transparent. Other than that the control if functional.</p>
  
  <p>What is wrong?</p>