简体   繁体   English

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

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

I am currently developing a WPF application. 我目前正在开发WPF应用程序。 When the user submits the form I have a method which checks to ensure that the fields are filled in and what I want to do is, if they are not, display a red to white gradient going around the box as a fade in animation. 当用户提交表单时,我有一种方法可以检查以确保填充了字段,而我想要做的是,如果没有填写,则在框周围显示从红色到白色的渐变,以淡入淡出的动画形式。

Is this possible? 这可能吗?

Use validation in your binding, and then assign a validation error template to the TextBox. 在绑定中使用验证,然后将验证错误模板分配给TextBox。 Here's one that does a red rectangle: 这是一个红色矩形的例子:

<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>

Add this to the binding: Validation.ErrorTemplate="{StaticResource errorTemplate} 将此添加到绑定:Validation.ErrorTemplate =“ {StaticResource errorTemplate}

You can use a style and use data trigger to achieve this. 您可以使用样式并使用数据触发器来实现此目的。 This way, when your textbox is empty, you will see a red border and light red background. 这样,当您的文本框为空时,您将看到一个红色边框和浅红色背景。 See the sample code below: 请参见下面的示例代码:

<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