开发者

Messagebox Popup to Confirm Datepicker Issue

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:开发者_StackOverflow中文版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?


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.


I blogged a workaround HERE that make the message box unnecessary by changing the work flow.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜