我在 Silverlight 2 中使用普通日期选择器。我将选定的日期绑定到一个值,当该值更改时,我会弹出一个消息框以确认他们想要更改该值。

然而,当我在日期选择器的值更改后立即使用消息框时,会发生奇怪的行为。日期选择器的弹出窗口不会关闭,如果您将鼠标悬停在日历上,它将选择一个日期,而无需单击鼠标。

此外,发生这种情况后,它似乎会影响绑定,并且在重新加载页面之前无法再次设置视图模型的属性。

这个问题相当具体,所以我附上了一个精简的例子。选择一个日期并按“确定”,然后将鼠标移到日历上以重现该日期。

我的 XAML -

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

我的代码背后 -

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

有什么想法或解决方法吗?

有帮助吗?

解决方案

嗯,这实际上并不少见。我的一位同事最近在 Windows 窗体应用程序中遇到了非常奇怪的问题,因为他使用 MessageBox 来响应第三方菜单控件的单击事件(在菜单被关闭之前)。

一个对他不起作用但可能对你很有效的建议是将呼叫“推送”给调度员。这样您的 SelectedDateChanged 处理程序将返回 消息框实际上被显示了。

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

但需要记住以下几点:

  • MessageBox.Show 在 Silverlight 中是独一无二的,因为它是创建模式对话框的唯一方法之一。与 Windows 窗体中的消息循环仍在运行不同,Silverlight 的 UI 线程在返回之前会停止。
  • 该事件已经在日期更改后发生,因此这不是确认更改的好方法。粗略地浏览一下文档表明没有相应的“更改”事件。
  • 根据具体情况,您可能最好使用 ChildWindow 而不是 MessageBox。这不会出现您所描述的问题,因为虽然它看起来是一个模式对话框,但事实并非如此。
scroll top