开发者

Forcing Binding Validation in WPF

开发者 https://www.devze.com 2023-01-15 14:25 出处:网络
I want to validate all the TextBox controls on the page load event. I\'m aware of one method to do this. Here\'s what I used.

I want to validate all the TextBox controls on the page load event. I'm aware of one method to do this. Here's what I used.

BindingExpression bx = myTextBox.GetBindingExpression( TextBox.TextProperty );

if(bx != null)
    bx.UpdateSource();

I have a custom ValidationRule that checks if a field is required or not for so开发者_运维技巧me of my TextBoxes. Now if I run this code on the page load, it doesn't work. However, if I put this code into a button click event instead, then it works!

How do I validate my controls on a control load?


Try putting the code inside a Dispatcher.Begininvoke

    Dispatcher.BeginInvoke(new Action(() => {
          BindingExpression bx = myTextBox.GetBindingExpression(TextBox.TextProperty);

          if (bx != null)
            bx.UpdateSource();
    }));
0

精彩评论

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