Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WPF绑定-如何确定对象无效以防止保存_.net_Wpf_Binding - Fatal编程技术网

.net WPF绑定-如何确定对象无效以防止保存

.net WPF绑定-如何确定对象无效以防止保存,.net,wpf,binding,.net,Wpf,Binding,我在WPF应用程序中有一个文本框,它绑定到实现IDataErrorInfo的Linq to Entities类上的属性。textbox绑定具有ValidatesOnExceptions=True和validateSondaerRors=True。当textbox绑定到integer属性并且用户输入文本时,textbox大纲显示为红色,因为我没有设置自定义样式 在我的保存方法中,我如何知道对象无效,无法保存?我更希望用户单击“保存”按钮,我可以通知他们问题,而不是禁用“保存”按钮 干杯 戴夫我还没

我在WPF应用程序中有一个文本框,它绑定到实现IDataErrorInfo的Linq to Entities类上的属性。textbox绑定具有ValidatesOnExceptions=True和validateSondaerRors=True。当textbox绑定到integer属性并且用户输入文本时,textbox大纲显示为红色,因为我没有设置自定义样式

在我的保存方法中,我如何知道对象无效,无法保存?我更希望用户单击“保存”按钮,我可以通知他们问题,而不是禁用“保存”按钮

干杯


戴夫

我还没有找到一个简单的方法。我在陷阱周围找到了一些代码,可以递归遍历表单上的所有控件,并确定其中是否有验证错误。我最终把它变成了一种扩展方法:

// Validate all dependency objects in a window
internal static IList<ValidationError> GetErrors(this DependencyObject node)
{
    // Check if dependency object was passed
    if (node != null)
    {
        // Check if dependency object is valid.
        // NOTE: Validation.GetHasError works for controls that have validation rules attached 
        bool isValid = !Validation.GetHasError(node);
        if (!isValid)
        {
            // If the dependency object is invalid, and it can receive the focus,
            // set the focus
            if (node is IInputElement) Keyboard.Focus((IInputElement)node);
            return Validation.GetErrors(node);
        }
    }

    // If this dependency object is valid, check all child dependency objects
    foreach (object subnode in LogicalTreeHelper.GetChildren(node))
    {
        if (subnode is DependencyObject)
        {
            // If a child dependency object is invalid, return false immediately,
            // otherwise keep checking
            var errors = GetErrors((DependencyObject)subnode);
            if (errors.Count > 0) return errors;
        }
    }

    // All dependency objects are valid
    return new ValidationError[0];
}

这比它应该做的工作多得多,但是使用扩展方法简化了一点。

您可以在
绑定上设置为
true
,然后在某个父元素上添加事件处理程序。无论何时添加或删除错误,都会触发该事件。

这是一个很好的解决方案。谢谢,好主意!一旦我将我的解决方案应用到我的代码中,我将发布它!
var errors = this.GetErrors();
if (errors.Count > 0)
{
    MessageBox.Show(errors[0].ErrorContent.ToString());
    return;
}