将验证错误传递给 WPF 中的 UI 元素?
我正在使用 IDataErrorInfo 验证 WPF 表单中的数据。我在演示者中实施了验证。
实际的验证正在发生,但应该更新 UI 并设置样式的 XAML 并未发生。
问题是:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
我对 Validation.Errors 的绑定不包含任何数据。如何从 Presenter 类获取此数据并将其传递给此 XAML 以更新 UI 元素?
编辑:
文本框:
<TextBox Style="{StaticResource textBoxInError}" Name="txtAge" Height="23" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150">
<TextBox.Text>
<Binding Path="StrAge" Mode="TwoWay"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
验证发生,但数据无效时应用的样式没有发生。
I am using IDataErrorInfo to validate my data in a form in WPF. I have the validation implemented in my presenter.
The actual validation is happening, but the XAML that's supposed to update the UI and set the style isn't happening.
Here it is:
<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
The problem is that my binding to Validation.Errors
contains no data. How do I get this data from the Presenter class and pass it to this XAML so as to update the UI elements?
EDIT:
Textbox:
<TextBox Style="{StaticResource textBoxInError}" Name="txtAge" Height="23" Grid.Row="3" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Width="150">
<TextBox.Text>
<Binding Path="StrAge" Mode="TwoWay"
ValidatesOnDataErrors="True"
UpdateSourceTrigger="PropertyChanged"/>
</TextBox.Text>
The validation occurs, but the style to be applied when data is invalid is not happening.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否在绑定表单时观察过输出窗口?通过在绑定发生时检查输出可以发现大量验证问题。
还有一个快速说明:
使用
Path=(Validation.Errors).CurrentItem.ErrorContent
而不是
当向控件提供有效值时,它将为您节省一些进一步的绑定异常
Have you watched the output window as your form is being bound? a significant number of validation issues can be found by reviewing the output as the binding occurs.
One quick note as well:
use
Path=(Validation.Errors).CurrentItem.ErrorContent
rather than
It will save you some further binding excecption when a valid value is provided to the control
我注意到你的风格还没有完全完成。
该样式需要一个定义“Validation.ErrorTemplate”的控件模板,以便在发生验证错误时起作用。尝试进行以下更改,看看效果如何。
Paul Stovell 在此处有一篇关于 WPF 验证的非常好的文章,其中将介绍大多数你需要的东西。我还在此处编写了一篇文章来简化您可能也喜欢的验证。
之前
之后
I noticed that your Style is not completely finished.
The Style needs a control template that defines a "Validation.ErrorTemplate" for it to work when a validation error occurs. Try making the following changes to see how it goes.
Paul Stovell has a very good article on WPF validation here that will cover most things you need. I have also written an article here to simplify validation that you might also like.
BEFORE
AFTER