本文介绍了在我单击其中的控件之前,弹出窗口不会失去焦点并关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个下拉控件,其中包含一个 ToggleButton 和一个带有 TabControl 的 Popup 控件.我的问题是,除非我点击了其中的某个控件,否则 Popup 不会自动关闭.

I'm trying to create a dropdown control consisting of a ToggleButton and a Popup control with a TabControl inside. My problem is, that the Popup doesn't close automatically unless I've clicked a certain control inside it.

考虑下面的示例,其中弹出窗口包含一个 TabControl,它本身在 TabItem 中包含一个日历控件.

Consider the example below where the popup contains a TabControl which itself contains a Calendar control inside a TabItem.

预期的行为是弹出窗口在失去焦点时关闭(即单击容器窗口),但为了让弹出窗口触发 LostFocus 事件并因此关闭,我必须单击日历上的箭头按钮之一先控制.

The expected behavior is that the Popup Closes whenever it loses focus (i.e. clicking the container window), but in order for the popup to fire a LostFocus event and thus closing, I have to click one of the arrow buttons on the Calendar control first.

<UserControl
        x:Class="DropDownExample"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <Grid>
        <ToggleButton x:Name="ToggleButton"
                      ClickMode="Press">Example</ToggleButton>
        <Popup x:Name="Popup"
               Placement="Bottom"
               AllowsTransparency="True"
               StaysOpen="False"
               PopupAnimation="Slide"
               FocusManager.IsFocusScope="false">
            <TabControl x:Name="TabControl"
                        MinHeight="200">
                <TabItem>
                    <Calendar />
                </TabItem>
            </TabControl>
        </Popup>
    </Grid>
</UserControl>

在 ToggleButton 的 Checked/Unchecked 事件中控制 Popup 的打开/关闭.

The opening/closing of the Popup is controlled in the Checked/Unchecked events of the ToggleButton.

推荐答案

问题在于 ToggleButtonClickMode=Press.设置 ClickMode=Release 修复了问题,Popup 在失去焦点时关闭.

The problem is in that the ToggleButton has ClickMode=Press. Setting ClickMode=Release fixes the problem and Popup closes on focus lost.

这篇关于在我单击其中的控件之前,弹出窗口不会失去焦点并关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 13:06