[英]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.