Search code examples
c#wpfpopupwindowwpf-controls

Button click event is not fired on first click when place inside popup


I have placed a calendar control and a button control inside a popup control.

The problem I am facing:

  1. Popup control is opened by an toggle button click.
  2. After opening the popup control, I tried to select the date in the calendar on mouse click, now the calendar receives focus.
  3. Now I'm trying to click the button in the popup, but the button click event fires only on the second click.

The problem is that on the first click the popup receives focus and only on the second click the event triggers.

Code

<Popup
    x:Name="Popup"
    Grid.Row="1"
    AllowsTransparency="True"
    Focusable="True"
    IsOpen="False"
    Placement="Bottom"
    PlacementTarget="{Binding ElementName=Border}"
    StaysOpen="False">
    <Border>
        <StackPanel>
            <Calendar></Calendar>
            <Button Click="Button_Click"></Button>
        </StackPanel>
    </Border>
</Popup>

Solution

  • Handle the SelectedDatesChanged event for the Calendar and call Mouse.Capture(null):

    private void Calendar_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {
        Mouse.Capture(null);
    }