ValidationRules without binding

你离开我真会死。 提交于 2019-11-30 11:24:43

This worked for me:

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

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!