实体类实现的IDataErrorInfo
我有实体类:
public class Project
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
此类通过 Castle DynamicProxy 拦截器实现 IDataErrorInfo 和 INotifyPropertyChanged 接口。我在视图模型中有此类的实例:
public class ProjectEditViewModel : Screen
{
public Project Project { get; set; }
....
}
我在视图中显示此数据:
<TextBox Grid.Row="0" Grid.Column="1" x:Name="Project_Name" Margin="4"/>
<TextBox Grid.Row="0" Grid.Column="2" x:Name="Project_Description" Margin="4"/>
INotifyPropertyChanged 效果很好,但 IDataErrorInfo 不行。看起来 IDataErrorInfo 只有在由 viewmodel 实现时才有效。有没有简单的方法可以对实体对象而不是视图模型进行验证?
通过这种面向方面的编程进行数据验证对我来说最有意义,所以我想使用它。
如果这与我的问题有某种联系,我将使用这种样式来显示验证错误:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
我对 XAML 的理解真的很差,如果我错过了一些明显的东西,那么抱歉。
我也在 CaliburnMicro 讨论中提出了这个问题: http://caliburnmicro.codeplex.com/discussions/338196
I have entity class:
public class Project
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
this class implements IDataErrorInfo and INotifyPropertyChanged interfaces via Castle DynamicProxy interceptor. I have instance of this class in view model:
public class ProjectEditViewModel : Screen
{
public Project Project { get; set; }
....
}
I am displaying this data in view:
<TextBox Grid.Row="0" Grid.Column="1" x:Name="Project_Name" Margin="4"/>
<TextBox Grid.Row="0" Grid.Column="2" x:Name="Project_Description" Margin="4"/>
INotifyPropertyChanged works well, but IDataErrorInfo not. It looks like IDataErrorInfo works only when it is implemented by viewmodel. Is there any simple way to have this validation on entity object instead of viewmodel?
Doing data validation by this aspect oriented programing makes the best sense to me so I would like to use it.
And if this is somehow connected with my problem, I am using this style to displaying validation errors:
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
My understanding of XAML is really poor so sorry if I missed something what is obvious.
I asked this question also in CaliburnMicro discussion: http://caliburnmicro.codeplex.com/discussions/338196
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用
Binding
绑定文本,则设置 Binding 的ValidatesOnDataErrors=True
属性。希望这会有所帮助。If you are using
Binding
to bind the Text then setValidatesOnDataErrors=True
property of the Binding. Hope this will help.