I have a set of controls bound to data, on which I would like to programmaticaly add validators to the bindings. Currently I'm able to iterate over the visual tree to find those controls with bindings, and also add my validators to these controls. But to further specify which controls should have specific validation I wanted to use styles. So my XAML looks like this:
<TextBox Name="someTextBox" Style="{StaticResource optionalNumericTextBox}" />
Here, the optionalNumericTextBox
style serves both adding a validation error template and as a decorator to indicate that this textbox should have the optional numeric validator applied.
The problem occurs when I'm traversing the visual tree, discovers a control with bindings, and then need to determine the style in use. Currently I've tried
dependencyObject.GetValue(FrameworkElement.StyleProperty)
which gives me a Style object but as far as I can tell, this object does not carry the 'optionalNumericTextBox' value. Is it even possible to determine the key or is this information lost in the XAML reader?