Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValidationRules without binding

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>
like image 401
anderi Avatar asked Jun 04 '11 09:06

anderi


3 Answers

This worked for me:

<TextBox.Text>
    <Binding RelativeSource="{RelativeSource Self}" Path="Text" UpdateSourceTrigger="LostFocus">
      <Binding.ValidationRules>
        <Filters:IntegersOnlyValidator/>
      </Binding.ValidationRules>
   </Binding>
 </TextBox.Text>
like image 63
anderi Avatar answered Nov 09 '22 00:11

anderi


Your code-behind should be as independent of the GUI as possible, so I would recommend you to create a property and bind to that. When you want to pass the text to the method, just pass the value of the property.

like image 24
svick Avatar answered Nov 09 '22 00:11

svick


You could bind to just any string, e.g. create one as the source for the binding:

xmlns:sys="clr-namespace:System;assembly=mscorlib.dll"
  <TextBox>
    <TextBox.Text>
      <Binding Path=".">
        <Binding.Source>
          <sys:String>Default Text</sys:String>
        </Binding.Source>
        <Binding.ValidationRules>
          <!-- Validation Rules -->
        </Binding.ValidationRules>
      </Binding>
    </TextBox.Text>
  </TextBox>
like image 28
H.B. Avatar answered Nov 08 '22 23:11

H.B.