Animate button background color on error

To indicate an error, I would like to temporarily change the background color of the button. I am new to WPF animation and could not find a simple example. And to complicate matters even further, I use a trigger to handle error notifications.

So here is my XAML, and I would like to know how to replace the background setter with animation (say, blink red three times in five seconds or something like that).

<UserControl>
  <UserControl.Resources>
    <Style x:Key="ErrorStyle" TargetType="Button">
      <!--Clear the default error template (a red border)-->
      <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
          <ControlTemplate>
            <AdornedElementPlaceholder />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
          <Setter Property="ToolTip" 
              Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
          <!--TODO: Replace with animation-->
          <Setter Property="Background" Value="Orange"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </UserControl.Resources>
  <Grid>
    <Button Command="{Binding ProgramCommand, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}" 
        Style="{StaticResource ErrorStyle}">
      _Program
    </Button>
  </Grid>
</UserControl>

I am also open to suggestions for a more efficient (simple) error notification.

+3
source share
3 answers

You can use Trigger.EnterActionsfor this.

<Trigger Property="Validation.HasError" Value="True">
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard>
                <ColorAnimation Storyboard.TargetProperty="Background.Color" Duration="0:0:0.3"
                                From="White" To="Red" RepeatBehavior="3x" AutoReverse="True"/>
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
</Trigger>

(It is assumed that the background will be SolidColorBrush, as it Storyboard.TargetPropertyindicates its property Color)


-,

<TextBox Text="{Binding TestInt}">
    <TextBox.Background>
        <LinearGradientBrush>
            <GradientStop Color="White" Offset="0"/>
            <GradientStop Color="White" Offset="1"/>
        </LinearGradientBrush>
    </TextBox.Background>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[0].Color" Duration="0:0:0.3"
                                                From="White" To="Red" RepeatBehavior="3x" AutoReverse="True"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>
+8

@H.B. , . , SolidColor . , , .

<UserControl>
  <UserControl.Resources>
    <Style Key="ErrorStyle" TargetType="Button">
      <!--Clear the default error template (a red border)-->
      <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
          <ControlTemplate>
            <AdornedElementPlaceholder />
          </ControlTemplate>
        </Setter.Value>
      </Setter>
      <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
          <Setter Property="ToolTip"
              Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[0].Color" To="Red" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="3x" />
                <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[1].Color" To="Red" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="3x" />
                <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[2].Color" To="Red" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="3x" />
                <ColorAnimation Storyboard.TargetProperty="Background.GradientStops[3].Color" To="Red" Duration="0:0:0.5" AutoReverse="True" RepeatBehavior="3x" />
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
        </Trigger>
      </Style.Triggers>
    </Style>
  </UserControl.Resources>
  <Grid>
    <Button Command="{Binding ProgramCommand, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"
        Style="{StaticResource ErrorStyle}">
      _Program
    </Button>
  </Grid>
</UserControl>
+3
<Trigger Property="Validation.HasError" Value="True">
   <BeginStoryboard>
      <Storyboard>
         <ColorAnimation Storyboard.TargetProperty="Background" From="White" 
            To="Red" Duration="0:0:1" AutoReverse="True" RepeatBehavior="3x"/>
      </Storyboard>
   </BeginStoryboard>
</Trigger>
0
source

All Articles