ElementHost 控件中的 WPF 验证
我有一个 WinForms 表单,其中包含 ElementHost 控件(其中包含 WPF UserControl)和“保存”按钮。
在 WPF UserControl 中,我有一个带有一些验证的文本框。 像这样的东西......
<TextBox Name="txtSomething" ToolTip="{Binding ElementName=txtSomething, Path=(Validation.Errors).[0].ErrorContent}">
<Binding NotifyOnValidationError="True" Path="Something">
<Binding.ValidationRules>
<commonWPF:DecimalRangeRule Max="1" Min="0" />
</Binding.ValidationRules>
</Binding>
</TextBox>
这一切都很好。 然而,我想要做的是在表单处于无效状态时禁用“保存”按钮。
任何帮助将不胜感激。
I've got a WinForms form that contains an ElementHost control (which contains a WPF UserControl) and a Save button.
In the WPF UserControl I've got a text box with some validation on it. Something like this...
<TextBox Name="txtSomething" ToolTip="{Binding ElementName=txtSomething, Path=(Validation.Errors).[0].ErrorContent}">
<Binding NotifyOnValidationError="True" Path="Something">
<Binding.ValidationRules>
<commonWPF:DecimalRangeRule Max="1" Min="0" />
</Binding.ValidationRules>
</Binding>
</TextBox>
This all works fine. What I want to do however, is disable the Save button while the form is in an invalid state.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这应该对您有所帮助:
然后您也许可以通过在用户控件上注册的事件通知主窗体有关更改的信息。
I think this should help you:
Then you could perhaps inform the mainform about a change with an event registered on the usercontrol.
好吧,我终于找到了解决我的问题的方法。
在 WPF 控件中,我将其添加到
Loaded
事件中。上面的
ValidateControl
定义如下:最后,我添加了一个名为
Validated
的事件,该事件在其事件参数中包含一个IsValid
布尔值。 然后我能够在我的表单上连接这个事件来告诉它该控件是否有效。如果有更好的方法我有兴趣学习。
Well, I've finally worked out a solution to my problem.
In the WPF control I added this to the
Loaded
event.Where
ValidateControl
above, is defined as this:Finally I added an event called
Validated
which contains anIsValid
boolean in its event args. I was then able to hook up this event on my form to tell it that the control is valid or not.If there is a better way I'd be interested to learn.