PropertyNameChanged pattern with custom control problem

回答済み PropertyNameChanged pattern with custom control problem

  • 2012年3月8日 17:52
     
     

    Hi,

    My company has a need for a date picker control that allows the date to be typed in free-text in a number of unusual formats. I tried doing this a number of ways but settled on inheriting a ComboBox and doing a few tricks to prevent the dropdown from dropping-down and instead showing a calendar. In OnValidating I parse what's in  MyBase.Text to see if it's a valid date.

    All this happens internally within the control and it just exposes a Value property of type Nullable(Of Date) (because the text may be empty) which gets/sets the date.  I also created a ValueChanged event to implement the PropertyNameChanged pattern.

    Everything worked OK in a non-databinding environment but when I came to use the control in a project that used databinding I noticed it didn't update the object it was bound to.  I eventually tracked this down to the ValueChanged event, if I remove this event it works correctly but if the event exists (even if it's not called) the databinding doesn't work.

    A sample project showing this behaviour can be found here http://www.colinbaker.me.uk/Sample.zip. It works in its current state and you can verify this by putting a breakpoint in the Start setter of the Meeting class. If you then uncomment the ValueChanged event in DateComboBox you'll notice it stops working (note: it may appear to be working because the date remains in the box but the breakpoint will not be hit).

    Can anyone explain why the event breaks databinding?

    Thanks in advance,

    Colin

すべての返信

  • 2012年3月9日 6:43
    モデレータ
     
     回答済み コードあり
        'Public Event ValueChanged(ByVal sender As Object, ByVal e As ValueChangedEventArgs)
        'Public Event InvalidDate(ByVal sender As Object, ByVal e As EventArgs)
    
        Public Event ValueChanged As EventHandler
        Public Event InvalidDate As EventHandler

    Correct the event declaration like this, instead of create a custom delete and event.

    http://msdn.microsoft.com/en-us/library/5z57dxz2.aspx#Y0


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us


  • 2012年3月9日 10:41
     
      コードあり

    Thanks Mike, that fixed it.

    Interestingly though, if I need to pass extra data via the EventArgs and follow the advice in the link you provided by creating a  ValueChangedEventHandler delegate, databinding breaks again.  Here's the updated declarations:

    Public Event ValueChanged As ValueChangedEventHandler
    Public Event InvalidDate As EventHandler
    
    Public Delegate Sub ValueChangedEventHandler(sender As Object, e As ValueChangedEventArgs)

    Any idea why?

    Thanks,

    Colin

  • 2012年3月13日 11:59
    モデレータ
     
     回答済み

    I don't know why, but changed the Event name to anything else, the code will also work good.

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us