mercredi 6 mai 2015

How to change the color of the checked/toggled button programatically

Let say I've a toggle button which is checked initially and the background color (the color when the button is checked/toggled, NOT the color of the button itself) is blue by default. How to change the background color of the checked/toggled button?

For example:

if (myCondition == true)
    myToggleBtn.Background = System.Windows.Media.Brushes.Red;

The code above is just change the color of the toggle button itself, but I want to change the color checked/toggled button.

Updated

<ToggleButton x:Name="Line1Btn" Content="Line 1" HorizontalAlignment="Left" Margin="8,84,0,0"
              VerticalAlignment="Top" FontFamily="Tahoma" FontSize="11" Height="26" Width="50"
              IsEnabled="True" BorderBrush="#FFC7C7C7" Click="Line1Btn_Click">
  <ToggleButton.Style>
    <Style TargetType="ToggleButton">
      <Setter Property="Background"> 
        <Setter.Value> 
          <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
            <GradientStop Color="#FFCFCFCF" Offset="0"/> 
            <GradientStop Color="White" Offset="1"/> 
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
      <Style.Triggers> 
        <Trigger Property="IsChecked" Value="True"> 
          <Setter Property="Background" Value="DarkSeaGreen"/> 
        </Trigger> </Style.Triggers>
    </Style>
  </ToggleButton.Style>
</ToggleButton>

Aucun commentaire:

Enregistrer un commentaire