WPF TextBox Binding Validation Rules not Firing on LostFocus When TextBox is Empty -


We all know how the sexy WPF authentication emerges outside the box. I am trying a very simple thing and for some reason it has always been unsuccessful. I have a text box and my only requirement is that the user gives some information in the textbox Textbox is bound to FirstName and LastName properties with a customer object.

Here is the XML code:

  & lt; Text box style = "{static resource text box style}" grid. Column = "1" grid. Rows = "0" height = "20" width = "100" margin = "10" & gt; & Lt; TextBox.Text & gt; & Lt; Binding path = "first name" & gt; & Lt; Binding.ValidationRules & gt; & Lt; Exceptional Rule / & gt; & Lt; /Binding.ValidationRules> & Lt; / Binding & gt; & Lt; /TextBox.Text> & Lt; / Text box & gt;  

Customer class is FirstName property here:

  Set the public string first name {get {return_firstName;} {if (String.IsNullOrEmptE (value)) Throw new ApplicationException ("FirstName can not be empty or empty!"); _firstName = value; OnPropertyChanged ("First"); }}  

Even if I am throwing an exception, if FirstName (value) is empty or empty, then it is handled only if I type something in the text box and then it I remove it and then close tabs because the property is dependent on the event that has changed. But even though I focused on that text box, which puts it on focus, it does not stop verification.

UPDATE:

One of the best ways to handle this problem is to specify a string. Empty the text boxes on the window. Loaded Event:

  Zero AddCustomerWindow_Loaded (Object Sender, RoutedEventArgs E) {// Get All Text Box and Set Assets for Empty String! TxtFirstName.Text = string Empty; TxtLastName.Text = string. Empty; }  

Here is the code to bind:

  public AddCustomerWindow () {InitializeComponent (); This.Loaded + = New Routing Event Handler (AddCustomerWindow_Loaded); GvAddCustomer.DataContext = New Customer (); }  

In my example, the XML code, I do not see any compulsions. I suppose that the text property of the text box is forced to FirstName?

Second: The first name has been initialized first, or is it received by the text box on null ? Compulsive binding for blank values ​​always gives weird behavior ...

Try saying this in the property:

  public string first name { Get {if (_firstName == zero) {_firstName = String.Empty; } Return _firstName; } Set {if (String.IsNullOrEmpty (value)) New ApplicationException ("FirstName can not be empty or empty!"); _firstName = value; OnPropertyChanged ("First"); }}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -