TextBox with validation loses ErrorTemplate on tab change

前端 未结 3 1633
孤街浪徒
孤街浪徒 2020-12-05 04:22

I have a TextBox with a validation rule that is on a tab of a TabControl. The default ErrorTemplate correctly shows (red border around TextBox) when the validation rule fail

3条回答
  •  既然无缘
    2020-12-05 04:44

    Just an addition for special cases: I was having similar problem and I am now using a solution similar to Dylan's code.

    The difference is that my TabItem contains GroupBox and the TextBox is inside it. In this case the AdornerDecorator has to be in the GroupBox itself, not a direct descendand of the TabItem.

    So this didn't work:

    
        
            
                
                    
                        ...
                    
                
            
        
    
    

    But this did:

    
        
            
                
                    
                        ...
                    
                
            
        
    
    

    I am adding it because I couldn't find the solution easily and even the documentation of AdornerLayer.GetAdornerLayer() (though not sure if it is applicable here) states This static method traverses up the visual tree starting at the specified Visual and returns the first adorner layer found. - but perhaps it also stops at some point, this is not clear from the docs.

提交回复
热议问题