开发者

wpf validation rules problem with textbox

I created a class IntegersValidationRule which inherits from ValidationRule. Now I don't know what code should I write in XAML. That's what I hav开发者_如何学JAVAe:

<TextBox Name="defaultTxt"
     Height="23" Width="200">
  <TextBox.Text>
    <Binding UpdateSourceTrigger="PropertyChanged">
      <Binding.ValidationRules>
        <what:IntegersValidationRule/>
      </Binding.ValidationRules>
    </Binding>
  </TextBox.Text>
</TextBox>

I know that either I'm so stupid that I can't understand in many tutorials what Path in Binding property means, why should we use Binding here when there's no binding required and what should I use instead of 'what' word inside Binding.ValidationRule.


what is an xmlns (see MSDN) which needs to point to the namespace in which your validation rule class is declared, e.g.

xmlns:what="clr-namespace:MyApp.MyValidationRules"

If you add no Path (- how about reading this if you do not understand it? -) the binding will bind to the current DataContext, whatever that may be in your case.


  • Question 1: validators work on bindings. That is why you specify the rule on a binding. As soon as then value will be updated to the source (object that the control binds to) the rule is checked.

  • Question 2: See H.B. 's answer

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜