WPF Popup 隐藏问题 [英] WPF Popup hiding problem
问题描述
假设您有一个用于打开 Popup
的 ToggleButton
,其行为与 ComboBox
等所有已知元素的行为相同.
Suppose you have a ToggleButton
for opening a Popup
, same behaviour as all known elements as ComboBox
etc.
...这是这段代码:
<ToggleButton x:Name="PART_OpenToggleButton"
Focusable="False"
IsChecked="False"
Template="{StaticResource MyToggleButton}">
<Grid>
<Popup x:Name="PART_PopupControl"
Style="{StaticResource MyPopupStyle}"
StaysOpen="False"
VerticalAlignment="Bottom"
IsOpen="False"
PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}" />
</Grid>
</ToggleButton>
然后在你后面的代码中使用 .IsOpen
用于 Popup
和 .IsChecked
用于 ToggleButton
.一切正常,但是当您打开 Popup
并在边界外单击时,问题就来了.Popup
将关闭,但 ToggleButton
保持选中状态.
Then in code behind you work with .IsOpen
for Popup
and .IsChecked
for ToggleButton
.
Everything works, but the problem arrives when you Open the Popup
and click outside the borders.
The Popup
will be closed but the ToggleButton
stays checked.
你不能在 PopupOnClosed
Handler 中设置 ToggleButton.IsChecked = false
,因为当你点击 ToggleButton
来关闭 >Popup
,Popup
自行关闭,设置 ToggleButton.IsChecked = false
但同时您点击 ToggleButton
并它尝试再次打开 Popup
.所以你不能关闭它.
You cannot set in the PopupOnClosed
Handler that ToggleButton.IsChecked = false
, because when you Click on the ToggleButton
to close the Popup
, the Popup
closes itself, sets ToggleButton.IsChecked = false
but at the sime time you clicked on the ToggleButton
and it tries to Open the Popup
again. So you cannot close it.
第一次切换按钮点击:
-> ToggleButton IsChecked = true
<小时>
第二次切换按钮点击:
2nd ToggleButtonClick:
-> ToggleButton IsChecked = false
-> ToggleButton IsChecked = true
<小时>
因此,如果您在弹出窗口打开时单击切换按钮,它会闪烁但保持打开状态.
So if you click on the Toggle Button while Popup being open, it blinks but stays open.
请问你是怎么解决这个问题的?
How would you solve this problem, please ?
已
在 MyWindow.XAML 中尝试这个并添加依赖属性 IsDropDownOpen请在后面的代码中:
Try this in a MyWindow.XAML and add the dependency property IsDropDownOpen in the code behind, please:
<Grid>
<ToggleButton x:Name="PART_OpenToggleButton"
Focusable="False"
Height="20"
Width="50"
IsChecked="{Binding ElementName=TestWindow, Mode=TwoWay, Path=IsDropDownOpen}">
<Grid>
<Popup x:Name="PART_PopupControl"
Width="100"
Height="100"
StaysOpen="False"
Focusable="False"
VerticalAlignment="Bottom"
IsOpen="{Binding ElementName=TestWindow, Path=IsDropDownOpen}"
PlacementTarget="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToggleButton, AncestorLevel=1}}">
</Popup>
</Grid>
</ToggleButton>
</Grid>
public bool IsDropDownOpen
{
get { return (bool)GetValue(IsDropDownOpenProperty); }
set { SetValue(IsDropDownOpenProperty, value); }
}
public static readonly DependencyProperty IsDropDownOpenProperty =
DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(Window), new UIPropertyMetadata(false));
推荐答案
我在这篇文章中找到了解决方案:https://stackoverflow.com/a/5821819/651161
I found the solution on this post : https://stackoverflow.com/a/5821819/651161
使用以下类将允许在按下切换按钮之前处理单击.弹出窗口因单击而关闭,但随后会处理单击,因此不会触发 ToggleButton 单击.
Using the following class will permit to handle the click before the togglebutton is pressed. The popup is closed because of the click but the click is then handled, so it doesn't trigger the ToggleButton click.
public class MyPopup : Popup {
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) {
bool isOpen = this.IsOpen;
base.OnPreviewMouseLeftButtonDown(e);
if (isOpen && !this.IsOpen)
e.Handled = true;
}
}
这篇关于WPF Popup 隐藏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!