WPF TextBox验证异常:边框变色后卡顿/占位符重叠问题求助
问题:TextBox商品编号验证UI异常解决
需求:TextBox输入商品编号(artikelnummer),若该编号对应的记录超过6个月,将TextBox边框设为红色,需通过SQL查询获取数据,业务逻辑已验证正确,但UI实现存在问题,尝试三种方案均异常:
方案1:TextChange事件实现
代码:
TextBox tbx = sender as TextBox; if (tbx.Text.Length > 7) { string getDate = SQL; try { using (var reader = command.ExecuteReader()) { if (reader.HasRows) { reader.Read(); date = reader.GetDateTime(0); } reader.Close(); } var lastmonth = DateTime.Today.AddMonths(-6); if (lastmonth < date) { tbx.BorderBrush = (Brush)Application.Current.MainWindow.FindResource("BorderBrushWrong"); } else { tbx.BorderBrush = (Brush)Application.Current.MainWindow.FindResource("BorderBrush"); } } catch { } }
问题:边框变红后,TextBox控件卡顿,无法正常切换焦点、选中文字,仅能通过删除键清除内容。
方案2:绑定属性+DataTrigger实现
属性代码:
private string _TopArtikel; public string TopArtikel { get { //replaceHTMLnew(); return _TopArtikel; } set { _TopArtikel = value; inputCheck(_TopArtikel); NotifyPropertyChanged(nameof(TopArtikel)); } }
inputCheck中设置colorcheck = true;,Style中添加DataTrigger:
<DataTrigger Binding="{Binding colorcheck}" Value="True"> <Setter Property="BorderBrush" Value="Red"/> </DataTrigger>
问题:出现与第一种方案相同的卡顿现象。
方案3:ValidationRule实现
验证类代码:
public override ValidationResult Validate(object value, CultureInfo cultureInfo) { NewsViewModel viewModel = new NewsViewModel(); string input = value.ToString(); if (input.Length > 7) { string getDate = SQL; DateTime date = new DateTime(); IGenericCommand command = viewModel.mandant.MainDevice.GenericConnection.CreateSqlStringCommand(getDate); command.AppendInParameter("artikelnummer", typeof(string), input); try { using (var reader = command.ExecuteReader()) { if (reader.HasRows) { reader.Read(); date = reader.GetDateTime(0); } reader.Close(); } var lastmonth = DateTime.Today.AddMonths(-6); if (lastmonth < date) { return new ValidationResult(false,""); } else { return new ValidationResult(true, null); } } catch { MessageBox.Show("Fehler"); } } return new ValidationResult(true, null); }
XAML绑定:
<Binding Path="TopArtikel" ValidatesOnExceptions="True" UpdateSourceTrigger="PropertyChanged"> <Binding.ValidationRules> <local:MonthValidation/> </Binding.ValidationRules> </Binding>
问题:TextBox占位符(WaterMarkLabel)不消失,输入内容与占位符重叠,边框未变色。
TextBox Style代码
<Style x:Key="txtStyle" TargetType="{x:Type TextBox}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="FontSize" Value="12"/> <Setter Property="BorderBrush" Value="#e5e7ea"/> <Setter Property="TextWrapping" Value="Wrap"/> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="BorderThickness" Value="2"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Grid> <Border x:Name="border" Grid.ColumnSpan="2" Margin="0 2 0 2 " Background="{Binding Background, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" CornerRadius="5" BorderThickness="1.5" BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/> <ScrollViewer Grid.Column="2" x:Name="PART_ContentHost" Margin="5,5,0,5" VerticalAlignment="Center" /> <Label Margin="5,0 0,0" HorizontalAlignment="Left" Grid.Column="1" x:Name="WaterMarkLabel" VerticalAlignment="Center" Visibility="Collapsed" Foreground="Gray" FontFamily="Arial" Content="{Binding Tag, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/> </Grid> <ControlTemplate.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="Text" Value=""/> </MultiTrigger.Conditions> <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/> </MultiTrigger> <DataTrigger Binding="{Binding colorcheck}" Value="True"> <Setter Property="BorderBrush" Value="Red"/> </DataTrigger> <Trigger Property="IsFocused" Value="True"> <Setter Property="Background" Value="White"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="DimGray"/> </Trigger> <Trigger Property="IsReadOnly" Value="True"> <Setter Property="Background" Value="#f0f0f0"/> <Setter Property="BorderThickness" Value="0"/> </Trigger> <EventTrigger RoutedEvent="TextBox.GotFocus"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#5B7FFF" Duration="00:00:00.2" /> </Storyboard> </BeginStoryboard> </EventTrigger> <EventTrigger RoutedEvent="TextBox.LostFocus"> <BeginStoryboard> <Storyboard> <ColorAnimation Storyboard.TargetName="border" Storyboard.TargetProperty="BorderBrush.Color" To="#e5e7ea" Duration="00:00:00.2" /> </Storyboard> </BeginStoryboard> </EventTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> <Condition Property="IsSelectionActive" Value="false"/> </MultiTrigger.Conditions> <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> </MultiTrigger> </Style.Triggers> </Style>
求助
寻求正确的TextBox验证实现方式,使输入超过6个月的商品编号时边框正常变红且无UI异常。
内容的提问来源于stack exchange,提问作者NadiaBallshek
相关产品推荐
相关产品推荐

