-
April 28th, 2013, 04:57 AM
#1
Why doesnt DataBindings with the DateTimePicker always work
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:
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));
}
}
}
I put 2 breakpoints on the places marked in the code sample above (bp1 and bp2).
Next step is binding the properties to the DateTimePicker. I do that with the next code:
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));
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.
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;
}
-
May 1st, 2013, 01:47 PM
#2
Re: Why doesnt DataBindings with the DateTimePicker always work
Originally Posted by Cornelis
Next step is binding the properties to the DateTimePicker. I do that with the next code:
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));
Code:
public static string GetNameOfProperty<T>(Expression<Func<T>> variabeleAccessExpression)
{
var memberExpression = variabeleAccessExpression.Body as MemberExpression;
return memberExpression.Member.Name;
}
Given how GetNameOfProperty works in the code above, your EndDatePicker.DataBindings.Add(...) calls are going to bind to the name of the method that calls them. Perhaps I'm wrong about this, but I would pull out the property retrieval code as a test and verify by setting a break point on the following line and make sure it is returning what I expect (i.e. the name of the property).
Code:
var endDatePickerPropertyName = Utilities.GetNameOfProperty(() => EndDatePicker.Value);
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|