如何强制树视图中的文本框进行验证(.UpdateSource()) [英] how to force textboxes in a treeview to validate (.UpdateSource())

查看:53
本文介绍了如何强制树视图中的文本框进行验证(.UpdateSource())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下方法通过树视图强制验证。



I have the following method I wrote to force validation through the treeview.

private bool WalkTreeValidation(ItemCollection treeViewItems)
       {
           bool validError = false;
           foreach (TreeViewItem node in treeViewItems)
           {
               object objTxtBox = node.FindName("orderNum");
               if (null != objTxtBox)
               {
                   TextBox txtBox = (TextBox)objTxtBox;
                   txtBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
                   validError = Validation.GetHasError(txtBox);
                   if (!validError && node.HasItems)
                       validError = validError || WalkTreeValidation(node.Items);
               }
           }

           return !validError;
       }







我这称之为




I'm calling it this

rtn = WalkTreeValidation(myTree.Items);





但是当我尝试迭代时,Items集合在foreach中,具有绑定的视图模型的集合,节点是TreeViewItem的集合。有没有帮助找到一种方法来强制在文本框上进行验证,这样如果有任何问题就会显示错误?



(项目可以进入树视图但无法保存不能再无效。)



however the Items collection when I try to iterate through in the foreach, has the collection of the viewmodels that are bound, node a collection of the TreeViewItem. Any help in finding a way to force the validation on the textboxes so they will display errors if there are any?

(Items can come into the treeview invalid but on save cannot be invalid anymore.)

推荐答案

您可以在ViewModel中使用与文本框关联的属性并实现INotitfyPropertyChanged并调用TextBox属性的NotifyPropertyChanged事件。



假设你的TextBox属性是ViewModel是TextBoxProperty



Public String TextBoxProperty

{

get {//};

set

{//

//你有逻辑检查如果值有效

//如果字符串中无效的reuturn validationerror

NotifyPropertyChanged(TextBoxProperty)

}

}



使用此TextBoxProperty代替有效ation logic
You can have a Property associated with Textbox in your ViewModel and Implement INotitfyPropertyChanged and Call the NotifyPropertyChanged Event of the TextBox Property.

Say your TextBox Property is ViewModel is "TextBoxProperty"

Public String TextBoxProperty
{
get{// };
set
{ //
// Have you logic for checking if the value is valid
// If not valid reuturn validationerror in a String
NotifyPropertyChanged("TextBoxProperty")
}
}

Use this TextBoxProperty in place of your validation logic


虽然这不是解决问题的理想方法,但我想要的是能够循环遍历树并手动触发验证规则,我作弊并做了一个黑客攻击。我所做的是收集所有TextBox的列表,因为它们被初始化并在保存循环中通过列表并触发验证规则。它工作得很好,但不是我想要的。
While this is not the ideal way to solve the problem, what I wanted was to be able to loop through the tree and manually trigger the validation rules, I cheated and made a hack. What I did was collect a list of all the TextBoxs as they were initialized and on save loop through the list and trigger the validation rules. It works just fine, but not how I would like it to.


这篇关于如何强制树视图中的文本框进行验证(.UpdateSource())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆