ScottSto
ScottSto

Reputation: 731

WPF Button not firing a single click event

I have a dialog box that contains a calendar control and a button. Once the user has selected a date on the calendar, they click the "Save" button and this performs an action and closes the dialog box:

<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>

The problem I am having is that after selecting a date, the button doesn't respond to a single click event - I Have to double click it in order to fire the click event. I have tried using the PreviewMouseLeftButtonDown, PreviewMouseDown and PreviewMouseUp events on the button, but none of them are able to accomplish this.

I have put a PreviewMouseDown on the element for testing, and it fires on the first click. Putting a preview event on the containing the button doesn't respond to the first click, though - I have to double click it just like the button.

I have also tried to put focus on the button after a date is selected:

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

This doesn't work either, unless I am debugging and put a break point in the StartDate_SelectedDatesChanged event. In this case, after I press F5 to continue, the button gets focus and the single click work. I've even tried putting a Thread.Sleep() statement in the StartDate_SelectedDatesChanged event, but that doesn't work either.

If this helps, this is how I am opening the window from the main screen. First, the method that opens the window:

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;
}

This is the code that calls the SetWindowPosition() to open the dialog box:

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

Any suggestions on how to get this to work?

Upvotes: 1

Views: 870

Answers (1)

mm8
mm8

Reputation: 169420

Try to call Mouse.Capture(null) in your StartDate_SelectedDatesChanged event handler.

Upvotes: 1

Related Questions