I have a weird problem with databindings and the datetimepicker in C# (winforms). Or at least I think it is weird.
I got a WinForm and on it I placed two DateTimePickers, one named BeginDatePicker, another one named EndDatePicker.
I also got a viewModel which has 2 properties declared:
I put 2 breakpoints on the places marked in the code sample above (bp1 and bp2).Code:private DateTime _beginDate; private DateTime _endDate; public DateTime BeginDate { get { return _beginDate; } set { if (value != _beginDate) //I have a breakpoint here, bp1 { _beginDate = value; RaisePropertyChanged(Utilities.GetNameOfProperty(() => BeginDate)); } } } public DateTime EndDate { get { return _endDate; } set { if (value != _endDate) //I have a breakpoint here: pb2 { _endDate = value; RaisePropertyChanged(Utilities.GetNameOfProperty(() => EndDate)); } } }
Next step is binding the properties to the DateTimePicker. I do that with the next code:
When I run my program and I change the date in the BeginDatePicker, nothing happens (the code doesnt break at breakpoint pb1.) When I change the date in the EnddatePicker, something happens, the code does break at breakpoint pb2.Code:BeginDatePicker.DataBindings.Add(Utilities.GetNameOfProperty(() => BeginDatePicker.Value), _viewModel, Utilities.GetNameOfProperty(() => _viewModel.BeginDate)); EndDatePicker.DataBindings.Add(Utilities.GetNameOfProperty(() => EndDatePicker.Value), _viewModel, Utilities.GetNameOfProperty(() => _viewModel.EndDate));
Why does the code not break at bp1 but it actually does break at bp2? I cannot figure out why. I checked it on typo's, I even write the code over again but no result.
the GetNameOfProperty Method is method I found on the internet:
Code:public static string GetNameOfProperty<T>(Expression<Func<T>> variabeleAccessExpression) { var memberExpression = variabeleAccessExpression.Body as MemberExpression; return memberExpression.Member.Name; }




Reply With Quote
