简体   繁体   English

在WPF中使用INotifyDataErrorInfo和嵌入式UserControl(与Caliburn.Micro)一起使用

[英]Using INotifyDataErrorInfo with embedded UserControl in WPF (with Caliburn.Micro)

I have inherited some code for a fairly complex WPF application which uses Caliburn.Micro to implementt the MVVM pattern. 我已经为使用Caliburn.Micro实现MVVM模式的相当复杂的WPF应用程序继承了一些代码。 It uses views bound automatically to view models using the Caliburn naming conventions. 它使用自动绑定的视图来使用Caliburn命名约定来查看模型。 The view models inherit from a class that implements INotifyDataErrorInfo, and Fluent Validation is used to generate field-specific errors. 视图模型从实现INotifyDataErrorInfo的类继承,并且Fluent Validation用于生成特定于字段的错误。 This works perfectly, except in one scenario where I have a user control embedded into several different views, and the UI elements within that user control need to be validated. 这非常完美,除了在一种情况下,我将用户控件嵌入到多个不同的视图中,并且需要验证该用户控件中的UI元素。

The embedded user control has it's own view model, which doesn't implement INotifyDataErrorInfo. 嵌入式用户控件具有其自己的视图模型,该模型不实现INotifyDataErrorInfo。 Suffice to say that the validation messages we generate when validating the parent view do not propagate up to the UI for display. 可以说我们在验证父视图时生成的验证消息不会传播到UI进行显示。

I'm not quite sure what code I would need to show here to illustrate the problem, so the question is more conceptual really - the question is: 我不太确定我需要在此处显示哪些代码来说明问题,因此该问题的确更具概念性-问题是:

Im my scenario, should I be implementing on the view model behind the embedded user control, or is there some other way to make the parent view call GetErrors() on it's view model with the property names of controls nested within embedded user controls? 在我的场景中,我应该在嵌入式用户控件后面的视图模型上实现,还是通过嵌套在嵌套用户控件内的控件的属性名称使父视图在其视图模型上调用GetErrors()?

I hope that makes sense - I'm quite new to WPF! 我希望这是有道理的-我对WPF还是很陌生!

I ended up solving this. 我最终解决了这个问题。 The problem turned out to be that I had a separate view model bound to the nested view, and that view model did not inherit from a class that implemented INotifyDataErrorInfo. 问题原来是我有一个绑定到嵌套视图的单独的视图模型,并且该视图模型没有继承自实现INotifyDataErrorInfo的类。 Suffice to say that the rule here seems to be you can have embedded user controls which have their own view model (datacontext) and still get the validation behaviour, but every view model in the hierarchy must implementINotifyDataErrorInfo otherwise the UI elements bound to them won't get notified of errors. 可以说这里的规则似乎是,您可以拥有具有自己的视图模型(数据上下文)的嵌入式用户控件,并且仍然具有验证行为,但是层次结构中的每个视图模型都必须实现INotifyDataErrorInfo,否则绑定到它们的UI元素将无法实现。不会收到错误通知。 In my case it was difficult to see problem because Caliburn.Micro and AutoFac were doing a lot of auto-wiring that was hard to see in a debug context. 在我的案例中,很难看到问题,因为Caliburn.Micro和AutoFac进行了很多自动装配,而这在调试环境中是很难看到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM