title: Custom Control Background Color in WPF
body: <p>I have created a custom control as you see below (Generic.xaml):</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:MyCC}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:MyCC}"&gt;
                  &lt;Grid Name="grd" Height="{Binding Height}" Width="{Binding Width}"&gt;
                      &lt;Rectangle Name="FirstRec" Fill="Blue"/&gt;
                      &lt;Rectangle Name="SecondRec" Fill="Black" Margin="1"/&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property = "IsMouseOver" Value = "True"&gt;
              &lt;Setter Property = "Background" Value = "Red" /&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>For <em>FirstRec</em> I want to get the Foreground color of the CC (instead of blue) and for the <em>SecondRec</em> I need to get the Background (instead of black). Therefore now the trigger does not work properly! I also don't want to bind the Height and Width of the Grid because the CC has its own height and width but I don't know how to use it! Would you please help me?!</p>
  
  <p>EDIT:</p>
  
  <p>It is actually a switch that has a status. if status == 0 it shows an empty rectangle. if status == 1 it shows a filled rectangle. if status == 3 || status == 4 it shows a red cross on it. here is the cc:</p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:BreakerCC}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:BreakerCC}"&gt;
                  &lt;Grid Name="grd" Background="{TemplateBinding Background}" &gt;
                      &lt;Rectangle Name="MainRectangle" StrokeThickness="1" Stroke="{TemplateBinding Foreground}"/&gt;
                      &lt;Path Name="Line1" Stroke="Red" StrokeThickness="1" Stretch="Fill"&gt;
                          &lt;Path.Data&gt;
                              &lt;LineGeometry StartPoint="0,0" EndPoint="1,1" /&gt;
                          &lt;/Path.Data&gt;
                          &lt;Path.Style&gt;
                              &lt;Style TargetType="Path"&gt;
                                  &lt;Style.Triggers&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x00"&gt;
                                          &lt;Setter Property="Visibility" Value="Hidden"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x01"&gt;
                                          &lt;Setter Property="Visibility" Value="Hidden"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x02"&gt;
                                          &lt;Setter Property="Visibility" Value="Visible"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x03"&gt;
                                          &lt;Setter Property="Visibility" Value="Visible"/&gt;
                                      &lt;/DataTrigger&gt;
                                  &lt;/Style.Triggers&gt;
                              &lt;/Style&gt;
                          &lt;/Path.Style&gt;
                      &lt;/Path&gt;
                      &lt;Path Name="Line2" Stroke="Red" StrokeThickness="1" Stretch="Fill"&gt;
                          &lt;Path.Data&gt;
                              &lt;LineGeometry StartPoint="0,1" EndPoint="1,0" /&gt;
                          &lt;/Path.Data&gt;
                          &lt;Path.Style&gt;
                              &lt;Style TargetType="Path"&gt;
                                  &lt;Style.Triggers&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x00"&gt;
                                          &lt;Setter Property="Visibility" Value="Hidden"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x01"&gt;
                                          &lt;Setter Property="Visibility" Value="Hidden"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x02"&gt;
                                          &lt;Setter Property="Visibility" Value="Visible"/&gt;
                                      &lt;/DataTrigger&gt;
                                      &lt;DataTrigger Binding="{Binding Path=Status}" Value="0x03"&gt;
                                          &lt;Setter Property="Visibility" Value="Visible"/&gt;
                                      &lt;/DataTrigger&gt;
                                  &lt;/Style.Triggers&gt;
                              &lt;/Style&gt;
                          &lt;/Path.Style&gt;
                      &lt;/Path&gt;
                  &lt;/Grid&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>And this is the definition in MainWindow.xaml</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;Style x:Key="230KV" TargetType="Control"&gt;
          &lt;Setter Property="Background" Value="Black"/&gt;
          &lt;Setter Property="Foreground" Value="Red"/&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="132KV" TargetType="Control"&gt;
          &lt;Setter Property="Background" Value="Black"/&gt;
          &lt;Setter Property="Foreground" Value="Green"/&gt;
      &lt;/Style&gt;
      &lt;Style x:Key="400KV" TargetType="Control"&gt;
          &lt;Setter Property="Background" Value="Black"/&gt;
          &lt;Setter Property="Foreground" Value="Purple"/&gt;
      &lt;/Style&gt;
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;StackPanel&gt;
          &lt;CC:BreakerCC Status="{Binding Status}" Style="{StaticResource 132KV}" Height="20" Width="20"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>But when the status is 3 or 4 i need to change the Foreground to Red and that doesn't work!</p>