繁体   English   中英

在C#wpf中在TextBox周围添加弹出边框动画

[英]Adding Popup Border Animation around TextBox in C# wpf

我目前正在开发WPF应用程序。 当用户提交表单时,我有一种方法可以检查以确保填充了字段,而我想要做的是,如果没有填写,则在框周围显示从红色到白色的渐变,以淡入淡出的动画形式。

这可能吗?

在绑定中使用验证,然后将验证错误模板分配给TextBox。 这是一个红色矩形的例子:

<ControlTemplate x:Key="errorTemplate">
    <Canvas Width="{Binding Path=AdornedElement.ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Adorner}}}" 
            Height="{Binding Path=AdornedElement.ActualHeight, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Adorner}}}">
        <Border BorderBrush="Red" BorderThickness="1" >
            <AdornedElementPlaceholder/>
        </Border>
    </Canvas>
</ControlTemplate>

将此添加到绑定:Validation.ErrorTemplate =“ {StaticResource errorTemplate}

您可以使用样式并使用数据触发器来实现此目的。 这样,当您的文本框为空时,您将看到一个红色边框和浅红色背景。 请参见下面的示例代码:

<Style x:Key="RequiredField" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">               
            <Setter Property="TextBox.BorderBrush" Value="{StaticResource MySolidBrush}" />            
            <Setter Property="TextBox.Background" Value="{StaticResource MyInnerBrush}"/>               
            <Setter Property="TextBox.ToolTip" Value="This Field is Mandatory"/>
        </DataTrigger>
    </Style.Triggers>
</Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM