WPF Nested binding in a controltemplate

前端 未结 1 1997
無奈伤痛
無奈伤痛 2021-01-13 16:26

I have successfully created a textbox that displays / collapses an error message depending upon a validation rule set in its model / vm. The code goes like this for the emai

1条回答
  •  礼貌的吻别
    2021-01-13 17:15

    You won't need the Grid.Row and Grid.Column bindings in the Template StackPanel since the StackPanel won't be the direct child of a Grid anyway,

    TemplateBinding is always a OneWay binding so the Text property for the Templated TextBox will never get updated. Change it to a regular Binding with RelativeSource and TwoWay

    Change ElementName=validableText to RelativeSource={RelativeSource TemplatedParent} in the bindings for ContentPresenter since we want to perform the validation check on the Templated TextBox and not the TextBox inside the Template.

    
        
            
            
                
                    
                        
                
            
        
    
    

    On a side note, another alternative that you have here is to create a UserControl with the original piece of Xaml that you had. You could introduce the Dependency Properties needed for your scenario (Text etc.). It would only require small changes.

    0 讨论(0)
提交回复
热议问题