ElementHost 控件中的 WPF 验证

发布于 2024-07-26 05:47:21 字数 604 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-08-02 05:47:21

我认为这应该对您有所帮助:

<UserControl Validation.Error="Validation_OnError >
<UserControl.CommandBindings>   
    <CommandBinding Command="ApplicationCommands.Save" CanExecute="OnCanExecute" Executed="OnExecute"/> 
</UserControl.CommandBindings> 
...
<Button Command="ApplicationCommands.Save" />
...
</UserControl>

/* put this in usercontrol's code behind */
int _errorCount = 0;
private void Validation_OnError(object sender, ValidationErrorEventArgs e)
{
    switch (e.Action)
    {
        case ValidationErrorEventAction.Added:
            { _errorCount++; break; }
        case ValidationErrorEventAction.Removed:
            { _errorCount--; break; }
    }
}

private void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = _errorCount == 0;
}

然后您也许可以通过在用户控件上注册的事件通知主窗体有关更改的信息。

I think this should help you:

<UserControl Validation.Error="Validation_OnError >
<UserControl.CommandBindings>   
    <CommandBinding Command="ApplicationCommands.Save" CanExecute="OnCanExecute" Executed="OnExecute"/> 
</UserControl.CommandBindings> 
...
<Button Command="ApplicationCommands.Save" />
...
</UserControl>

/* put this in usercontrol's code behind */
int _errorCount = 0;
private void Validation_OnError(object sender, ValidationErrorEventArgs e)
{
    switch (e.Action)
    {
        case ValidationErrorEventAction.Added:
            { _errorCount++; break; }
        case ValidationErrorEventAction.Removed:
            { _errorCount--; break; }
    }
}

private void OnCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = _errorCount == 0;
}

Then you could perhaps inform the mainform about a change with an event registered on the usercontrol.

奶茶白久 2024-08-02 05:47:21

好吧,我终于找到了解决我的问题的方法。

在 WPF 控件中,我将其添加到 Loaded 事件中。

Validation.AddErrorHandler(this.txtSomething, ValidateControl);

上面的 ValidateControl 定义如下:

private void ValidateControl(object sender, ValidationErrorEventArgs args)
{
    if (args.Action == ValidationErrorEventAction.Added)
       OnValidated(false);
    else
       OnValidated(true);
}

最后,我添加了一个名为 Validated 的事件,该事件在其事件参数中包含一个 IsValid 布尔值。 然后我能够在我的表单上连接这个事件来告诉它该控件是否有效。

如果有更好的方法我有兴趣学习。

Well, I've finally worked out a solution to my problem.

In the WPF control I added this to the Loaded event.

Validation.AddErrorHandler(this.txtSomething, ValidateControl);

Where ValidateControl above, is defined as this:

private void ValidateControl(object sender, ValidationErrorEventArgs args)
{
    if (args.Action == ValidationErrorEventAction.Added)
       OnValidated(false);
    else
       OnValidated(true);
}

Finally I added an event called Validated which contains an IsValid 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文