My ToggleButton become "green" when "PauseUpdate" is false but otherwise it stay blue (like when the focus is on) althought I put the focus elsewhere. It never becomes red.
Also, my text is always "Pause", never "Paused". Why?
<StackPanel HorizontalAlignment="Right">
<ToggleButton HorizontalAlignment="Right" VerticalAlignment="Top" Margin="7" Padding="7" IsChecked="{Binding PauseUpdate}">
<TextBox Text="Pause" IsReadOnly="True" Background="Transparent" BorderThickness="0">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding PauseUpdate}" Value="true">
<Setter Property="Text" Value="Paused" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
<ToggleButton.Background>
<SolidColorBrush Color="Green" Opacity=".2"></SolidColorBrush>
</ToggleButton.Background>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Style.Triggers>
<DataTrigger Binding="{Binding PauseUpdate}" Value="true">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Red" Opacity=".2"></SolidColorBrush>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding PauseUpdate}" Value="false">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="Green" Opacity=".2"></SolidColorBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<TextBox Width="60"></TextBox>
</StackPanel>
There are multiple points to be fixed:
TextBoxconsumes mouse events and thus preventsToggleButtonfrom responding to them. Moreover, it is just used to show a text. SoTextBlockis appropriate.A local value prevails over the value set in Style and thus prevents Style.Triggers from changing actual value. So remove the local value and set the value in Style.
The
BackgroundwhenIsCheckedis true is given by default ControlTemplate ofToggleButton. To override it, set a plain ControlTemplate inTemplateof Style.Then, the modified
ToggleButtonwould be as follows: