Why doesnt DataBindings with the DateTimePicker always work
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Why doesnt DataBindings with the DateTimePicker always work

  1. #1
    Join Date
    Apr 2013
    Posts
    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;
            }

  2. #2
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,958

    Re: Why doesnt DataBindings with the DateTimePicker always work

    Quote Originally Posted by Cornelis View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center