WPF按钮不触发单击事件

问题描述 投票:1回答:1

我有一个对话框,其中包含一个日历控件和一个按钮。用户在日历上选择日期后,他们单击“保存”按钮,这将执行操作并关闭对话框:

<Window...>
    <Grid x:Name="CalendarGrid">
        <Grid.ColumnDefinitions>...</Grid.ColumnDefinitions>
        <Grid.RowDefinitions>...</Grid.RowDefinitions>
        <StackPanel Grid.Column="0" Grid.Row="0" x:Name="StackPanel1">
            <Calendar x:Name="StartDate" HorizontalAlignment="Center" SelectedDatesChanged="StartDate_SelectedDatesChanged" />
        </StackPanel>
        <StackPanel Grid.ColumnSpan="2" Grid.Row="1">
            <Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click" Width="50" IsDefault="True" ClickMode="Press" />
        </StackPanel>
    </Grid>
</Window>

我遇到的问题是,选择日期后,该按钮无法响应单击事件-我必须双击它才能触发单击事件。我已经尝试在按钮上使用PreviewMouseLeftButtonDown,PreviewMouseDown和PreviewMouseUp事件,但是它们都无法完成此任务。

我已将PreviewMouseDown放在要测试的元素上,并在第一次单击时触发。但是,在包含按钮的预览事件上放置按钮并不会响应第一次单击-我必须像按钮一样双击它。

选择日期后,我还尝试将焦点放在按钮上:

    private void StartDate_SelectedDatesChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        SaveButton.Focus();
    }

这也不起作用,除非我正在调试并在StartDate_SelectedDatesChanged事件中放置一个断点。在这种情况下,按F5继续后,按钮将获得焦点并且单击即可工作。我什至尝试过在StartDate_SelectedDatesChanged事件中放入Thread.Sleep()语句,但这也不起作用。

如果有帮助,这就是我从主屏幕打开窗口的方式。首先,打开窗口的方法:

public static void SetWindowPosition(Window dialog)
{
    Window mainWindow = Application.Current.MainWindow;

    dialog.Owner = mainWindow;
    dialog.Left = mainWindow.Left + (mainWindow.ActualWidth - dialog.ActualWidth) / 2;
    dialog.Top = mainWindow.Top + (mainWindow.ActualHeight - dialog.ActualHeight) / 2.5;
}

这是调用SetWindowPosition()以打开对话框的代码:

 var dialog = new CalendarDialog();
 dialog.SetValues(Phrases.SelectDateRange, Phrases.StartingDate, Phrases.EndingDate, null, null);
 SetWindowPosition(dialog);

有关如何使其正常工作的任何建议?

wpf
1个回答
1
投票

尝试在Mouse.Capture(null)事件处理程序中调用StartDate_SelectedDatesChanged


0
投票

它是否也在其他工作站中发生,还是仅在您的计算机中发生?我有一些,而我们只是图形驱动程序已经过时了,搞乱了焦点行为。

© www.soinside.com 2019 - 2024. All rights reserved.