假设你有一个ToggleButton
打开一个Popup
,相同的行为,所有已知的元素ComboBox
等WPF弹出隐藏的问题
...这是该代码:
<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
for ToggleButton
。 一切正常,但当您打开Popup
并点击边界外时,问题就会到达。 Popup
将被关闭,但ToggleButton
停留检查。
你不能在PopupOnClosed
处理器是ToggleButton.IsChecked = false
设置,因为当你点击ToggleButton
关闭Popup
,在Popup
自行关闭,设置ToggleButton.IsChecked = false
但在森那一次你点击了ToggleButton
并尝试打开再次Popup
。所以你不能关闭它。
1 ToggleButtonClick:
-> ToggleButton IsChecked = true
第二ToggleButtonClick:
-> ToggleButton IsChecked = false
-> ToggleButton IsChecked = true
所以,如果你点击切换按钮,同时弹出是开放的,它闪烁但保持打开状态。
请问您如何解决此问题?
编辑:
在MyWindow.XAML试试这个,在代码添加依赖属性IsDropDownOpen 后面,请:
<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));
取看看:http://stackoverflow.com/questions/13687463/wpf-popup-staysopen-false-still-keep-the-popup-open-while-clicking-outside – SepehrM 2015-01-25 17:23:19