【发布时间】:2025-11-23 11:30:02
【问题描述】:
我正在创建一个自定义文本框控件以具有一些默认格式设置和验证功能,包括一个接受动作或自定义函数的自定义验证属性。
在我目前的情况下,我无法使用某些人发布的属性验证框架,因为我没有权限修改数据模型对象。我也不能使用 ValidationRules,因为并非所有需要这些验证的文本框都会被绑定。
我在静态构造函数中使用了通用文本框样式,而不是自己定义,但我添加了一些数据触发器来根据我的自定义 IsValid 依赖属性设置边框和工具提示。
一切似乎都运行良好,但问题是当我将鼠标悬停在或单击验证失败的文本框时,“无效”样式消失并变为默认文本框样式。
我尝试为 IsMouseOver、IsFocused 和 IsMouseCaptured 事件(如下所示)创建一些额外的数据触发器,但无济于事。
我错过了什么吗?
静态构造函数(显示我使用的是 TextBox 样式:
static ValidatorTextBox()
{
//Commenting this line out to use the default textbox style
DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidatorTextBox), new FrameworkPropertyMetadata(typeof(TextBox)));
}
这是我的风格:
<Style TargetType="{x:Type local:ValidatorTextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsValid}" Value="False">
<Setter Property="BorderBrush" Value="Red" />
<Setter Property="BorderThickness" Value="1" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsRequired}" Value="True">
<Setter Property="Background" Value="AliceBlue" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsFocused}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseOver}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsMouseCaptured}" Value="True">
<Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=BorderBrush}" />
</DataTrigger>
</Style.Triggers>
</Style>
【问题讨论】:
标签: wpf validation custom-controls