views:

210

answers:

1

I am using the vanilla datepicker in Silverlight 2. I bind the selected date to a value, and when that value changes I pop a messagebox to confirm that they would like to change the value.

However strange behaviour ensues when I use a messagebox straight after the datepicker's value is changed. The datepicker's popup will not close, and if you mouse over the calendar it will choose a date without having to click the mouse.

Also, after this occurs it seems to affect bindings and it cannot set the view model's property again until the page is reloaded.

This problem is rather specific so I have attached a stripped down example. Choose a date and press OK then move your mouse over the calendar to reproduce this.

My XAML -

<Grid x:Name="LayoutRoot">
    <controls:DatePicker x:Name="dpTest" 
                         Height="25" 
                         Width="75" 
                         SelectedDateChanged="DatePicker_SelectedDateChanged" />
</Grid>

My code behind -

  Private Sub DatePicker_SelectedDateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs)
    MessageBox.Show("Test Popup")
End Sub

Any ideas or workarounds?

+1  A: 

Hmm this is not all that uncommon actually. A coworker of mine recently ran into very strange issues in a Windows Forms application because he was using MessageBox in response to a third party menu control's click event (before the menu had been dismissed.)

One suggestion that didn't work for him but may very well work for you is to "push" the call onto the dispatcher. That way your SelectedDateChanged handler will return before the message box actually gets shown.

Private Sub DatePicker_SelectedDateChanged( ... )

    ' Unfortunately my VB is rusty '
    ' I believe this is the correct syntax. '
    Dispatcher.BeginInvoke(AddressOf ShowDateMessage)

    ' At this point, the message box has *not* been shown '
    ' It will be shown once control returns to the dispatcher '

End Sub

Private Sub ShowDateMessage()

    ' By this point, the DatePicker popup should be closed '
    ' so hopefully the issues you are seeing would be avoided '
    MessageBox.Show("Test Popup")

End Sub

A couple of things to keep in mind though:

  • MessageBox.Show is unique in Silverlight in that it is one of the only ways to create a modal dialog. And unlike in Windows Forms where the message loop is still running, Silverlight's UI thread is stalled until it returns.
  • The event already takes place after the date has changed so this is not a good way to confirm the change. A cursory glance at the docs suggests there isn't a corresponding "Changing" event.
  • Depending on the circumstances, you might just be better off using a ChildWindow instead of MessageBox. This wouldn't have the issues you described because while it appears to be a modal dialog, it's not.
Josh Einstein
A combination of your two suggestions above got me somewhere.If you push the MessageBox call onto the dispatcher, that does not solve the problem.If you pop a child window similar as to this blog describes - http://www.wintellect.com/CS/blogs/jprosise/archive/2009/04/29/silverlight-3-s-new-child-windows.aspxYou will find that you will have similar problem as if you popped a MessageBox.However! If you pop a child window onto the dispatcher all is well.Thanks for your help.
Wilson