ValidationRules without binding

前端 未结 3 516
野的像风
野的像风 2021-02-12 19:31

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 t

相关标签:
3条回答
  • 2021-02-12 20:08

    This worked for me:

    <TextBox.Text>
        <Binding RelativeSource="{RelativeSource Self}" Path="Text" UpdateSourceTrigger="LostFocus">
          <Binding.ValidationRules>
            <Filters:IntegersOnlyValidator/>
          </Binding.ValidationRules>
       </Binding>
     </TextBox.Text>
    
    0 讨论(0)
  • 2021-02-12 20:08

    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.

    0 讨论(0)
  • 2021-02-12 20:16

    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>
    
    0 讨论(0)
提交回复
热议问题