Анимация textbox'а при валидации вводимых данных в textbox с помощью INotifyDataErrorInfo
Как сделать валидацию данных вводимых в texbox в VM с помощью INotifyDataErrorInfo с анимацией textbox'a?
Если textbox будет пустым, то его background должен сразу стать красного цвета и в течении 2 сек. плавно окраситься в прежний цвет. Валидация должна происходить по нажатию кнопки. На данный момент, когда textbox пустой и я нажимаю кнопку, то background textbox'а становится красным через Trigger.EnterActions, но как background с красного цвета плавно окрасить в прежний цвет через Trigger.ExitActions(ведь нужно через какое то время делать ErrorsChanged?.Invoke, когда HasErrors = false) я не знаю.
VM:
public class LoginPageViewModel : BaseViewModel, INotifyDataErrorInfo
{
public LoginPageViewModel(MainViewModel mainViewModel)
{
MainViewModel = mainViewModel;
GoCommand = new RelayCommand(CheckCredentials);
}
public MainViewModel MainViewModel { get; }
public ICommand GoCommand { get; }
private string text;
public string Text
{
get { return text; }
set
{
SetField(ref text, value);
}
}
public bool HasErrors { get; set; } = false;
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
if (string.IsNullOrEmpty(propertyName) || (!HasErrors))
return null;
return new List<string>() { "Invalid credentials." };
}
public void CheckCredentials(object obj)
{
if (String.IsNullOrEmpty(Text)) HasErrors = true;
else { HasErrors = false; }
if (HasErrors)
{
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs("Text"));
}
}
}
Шаблон textbox'а:
<Style x:Key="TextBoxMain" TargetType="TextBox">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderBrush" Value="#FFD3D9DE"/>
<Setter Property="Border.CornerRadius" Value="4"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="Cursor" Value="IBeam"/>
<Setter Property="FontSize" Value="15"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Border x:Name="border" BorderThickness="{TemplateBinding BorderThickness}" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" CornerRadius="{TemplateBinding Border.CornerRadius}" SnapsToDevicePixels="True">
<Grid>
<ScrollViewer Margin="6,0,2,0" VerticalAlignment="Center" x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Panel.ZIndex="1"/>
<TextBlock Visibility="Collapsed" Foreground="Gray" x:Name="placeholder" FontSize="{TemplateBinding FontSize}" Margin="8,0,0,0" VerticalAlignment="Center" HorizontalAlignment="Left" Text="{TemplateBinding Tag}" Panel.ZIndex="0"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="GotFocus">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Foreground.Opacity" Storyboard.TargetName="placeholder" To=".65" Duration="0:0:0.1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="LostFocus">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Foreground.Opacity" Storyboard.TargetName="placeholder" To="1" Duration="0:0:0.1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
<Trigger Property="Text" Value="">
<Setter Property="Visibility" TargetName="placeholder" Value="Visible"/>
</Trigger>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red" />
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="border" From="Transparent" To="#19FF0000" Duration="0:0:0.05"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<AdornedElementPlaceholder Name="adornerPlaceholder"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Ответы (1 шт):
Автор решения: VeNNoM
→ Ссылка
Триггер в шаблоне:
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red" />
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:2">
<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="border" From="Transparent" To="#19FF0000" Duration="0:0:0.0"/>
<ColorAnimation Storyboard.TargetProperty="Background.Color" Storyboard.TargetName="border" BeginTime="0:0:1" From="#19FF0000" To="Transparent" Duration="0:0:0.2"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
Метод валидации в VM:
public void CheckCredentials(object obj)
{
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs("Text"));
if (String.IsNullOrEmpty(Text)) HasErrors = true;
else { HasErrors = false; }
if (HasErrors)
{
ErrorsChanged?.Invoke(this, new DataErrorsChangedEventArgs("Text"));
HasErrors = false;
}
}
