问题描述
我使用MVVM,并且我的对象实现了IDataErrorInfo.设置属性后,我将运行自定义验证方法,如果验证通过,则返回String.empty,它将Validation.HasError设置为false.如果验证失败,则将Validation.HasError设置为true.我有一种样式用于必需的控件"(将执行验证的控件),并将控件的ToolTip设置为类似这样的错误:
I use MVVM and my object implement IDataErrorInfo. When a property is set, I run custom validation methods and if the validation passes, I return String.empty, which sets Validation.HasError to false. If the validation fails, Validation.HasError is set to true. I have a style that I use for "required controls" (controls that will perform the validation) and set's the ToolTip of the control to whatever the error is like this:
<Style x:Key="RequiredControl" TargetType="{x:Type Control}" >
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding (Validation.Errors), Converter={StaticResource ErrorConverter}, RelativeSource={x:Static RelativeSource.Self}}"/>
</Trigger>
</Style.Triggers>
</Style>
和ErrorConverter:
And the ErrorConverter:
public class ZynErrorContentConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var errors = value as ReadOnlyObservableCollection<ValidationError>;
if (errors == null) return "";
return errors.Count > 0 ? errors[0].ErrorContent : "";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
问题是这样的:用户输入的内容无效...并且Validation.HasError设置为true.工具提示会按预期更新.如果用户尝试纠正错误,但输入的值导致其他类型的失效,则工具提示应显示新的错误字符串,但这不会发生.该错误显示为与第一个错误相同的错误.我知道为什么会这样(我认为)...因为没有触发触发器,因为Validation.HasError从未从True-> False-> True更改.
The problem is this: The user enters something invalid...and the Validation.HasError is set to true. The tooltip updates as it is supposed to. If the user attempts to correct the error, but enters a value that causes a different type of invalidation, the tooltip should show the new error string, but this doesn't happen. The error shows as the same error from the first error. I know why this happens (I think)...Because the Trigger is not triggered because the Validation.HasError never changes from True -> False -> True.
有人对此有任何经验或关于如何触发扳机的建议吗?
Does anyone have any experience with this or some advice as to how to force the trigger?
推荐答案
这似乎是答案:
基本上,您绑定到当前项目并使用ContentPresenter来显示错误.它适用于我的代码.
Basically, you bind to the current item and use a ContentPresenter to display the error. It worked for my code.
这篇关于如果已经发生新错误,则Validation.HasError不会再次触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!