How do I get the default tab order in WPF (i.e. Visual Tree order)?

200 views Asked by At

I'm adding a validation module to my application which will guide the user to the inputs that need attention. I would like to go through these controls in the same order that they were added to the Visual Tree. The Tab Index for each control is set to Int Max, which means just go in order of the Visual Tree.

My validation module just gets a bunch of controls to validate. It needs to order them from top to bottom so the user can go sequentially through the form. I would like to preserve the original tabbing behavior. I just need to know the visual tree order of the controls in my list so I can sort them properly.

1

There are 1 answers

0
NielW On

After a couple days of research, I realized this is not provided out of the box. In the end, I just called FindVisualChildren to get all FrameworkElements, and if the control implemented my IValidatable interface, I assigned it the next available index from a counter. I had to add a dependency property called Order to make it work (I didn't want to muck with the existing TabIndex). Then, when I got my list of controls needing validation, I just sorted them by Order.

This is definitely not a scalable solution, but luckily I only need to run it once.

If anyone has a better solution, PLEASE post it. I feel dirty.