Williamq

Hi guys

I have a class which has a Dob (date of birth) property with validation rule (If user select a date early than 1920/01/01, reset the value to 1920/01/01. If user select a date later than .... ) inside it.

Then I bind it to a Windows.Forms.DateTimePicker control and found out that the validation rule works but the new value won't be displayed on the DateTimePicker control until I manually call Form.validatechildren or Bindsourse.ResetItem method in Validated event of the DateTimePicker control.

What do you guys think

By the way, Yes, I do have INotifyPropertyChanged interface in the class and it works very well except the above problem. And also I know I can use the MaxDate/MinDate properties of DateTimePicker control instead of using validation rule inside the class. But in that way, I guess end users will have a little bit of unhappy experience.For example, MaxDate is Jan-01-1980, the current value is Jan-01-1980 and the user want to change it to Feb-01-1960. Clicking on the Jan to change it won't work until 1980 is changed.




Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

tkerns

Show your code for the property and the validation rule.

Tony





Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

Williamq

Here is the code:

public DateTime Dob{

get{return this._dob;}

set

{

DateTime newValue = value;

if (newValue < minDob){newValue = minDob;}

else if (newValue > maxDob){newValue = maxDob;}

if (this._dob != newValue){this._dob = newValue;OnPropertyChanged();}

}

}






Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

Williamq

No one knows

Or using Bindsourse.ResetItem method in Validated event is the best answer already






Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

timvw

What does your OnPropertyChanged method look like

Overhere it looks like: (untested, by heart)

private void OnPropertyChanged(string propertyName) {
EventHandler<PropertyChangedEvent> handler = this.PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName);
}
}


And then in each property, i call the OnPropertyChanged, in your case that would be:
this.OnPropertyChanged("Dob"); It's essential that the propertyName is 100% correct ;) (case sensitive too)





Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

Williamq

Hi timvw

Thanks for your reply. I double checked the code inside the Dob property. Everything looks fine. I even created a test button on the form which calls "bindingsource.current.Dob = new DateTime(XX/XX/XXXX)". It changes the backend data value and displays the new date on the form immediately. Even the new date value is outside the scope and validation rule code executed.

But the "problem" in my case still remains. For example, I changed the year from 1970 to 2050 by using datetimepicker control. The backend data has been reseted to 1986 but won't be displayed only if I put Bindsourse.ResetItem method in Validated event of the datetimepicker control.

-----------------------------------------------------------------------------------------------------------------------------------------------

About the OnPropertyChanged method, I would prefer to use

protected void OnPropertyChanged(){

string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);

OnPropertyChanged(propertyName);}

to avoid typo. And use (this.PropertyChanged != null) directly to avoid unnecessary new instance which has the same reference value with the existing one. What do you think






Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

timvw

If i understand well, you want to commit/push the selected value sooner (before the user leaves moves the focus to another control)... Handling the Close_Up event on the DateTimePicker can accomplish this:

private void dateTimePicker1_CloseUp(object sender, EventArgs e)
{
foreach (Binding binding in this.dateTimePicker1.DataBindings)
{
binding.WriteValue();
}
}





Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

timvw

In the case where PropertyX is dependend on PropertyY you would want to raise the PropertyChanged event for both properties (But for simple properties, I find using the StackTrace to get at the propertyName an original solution :))






Re: Windows Forms Data Controls and Databinding Two way databinding doesn't work?

Williamq

No, I don't want to push the selected value sooner. And I don't even use the drop-down calendar due to the well-known UICulture issue.

Ideally, User can input what ever date they want as they are still working on that control (Control has focus). But when they move the mouse to another control (Control lost focus), I want databinding & validation rule works and then the new value display back on the control.