ValidationRules without binding

anderi picture anderi · Jun 4, 2011 · Viewed 9.3k times · Source

I want to use the ValidationRules (and it's UI effects) on the textbox without actually binding anything to the textbox.

I want to use the textbox for some input that doesn't bound to anything but need to validate the input after focus is lost using the ValidationRules.

Can it be done?

<TextBox.Text>
   <Binding Path="" UpdateSourceTrigger="LostFocus">
     <Binding.ValidationRules>
        <local:IntegersOnlyValidator/>
     </Binding.ValidationRules>
   </Binding>
 </TextBox.Text>

Answer

anderi picture anderi · Jun 8, 2011

This worked for me:

<TextBox.Text>
    <Binding RelativeSource="{RelativeSource Self}" Path="Text" UpdateSourceTrigger="LostFocus">
      <Binding.ValidationRules>
        <Filters:IntegersOnlyValidator/>
      </Binding.ValidationRules>
   </Binding>
 </TextBox.Text>