简体   繁体   中英

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM