Click here to Skip to main content
15,905,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following method I wrote to force validation through the treeview.

C#
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
C#
rtn = WalkTreeValidation(myTree.Items);


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.)
Posted

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
 
Share this answer
 
Comments
bowlturner 27-Nov-13 8:57am    
Yes, that could work, but it's really a hack. I'm using validationRules and they work great as they are, I just need to be able to manually trigger them as well.

I came up with a Hack of my own that I'll share.
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900