积极答复者
求专家帮忙 控件模板中 POPUP 控件如何自动关闭

问题
-
答案
-
你好 IT农夫 ,
据我所知,Popup在打开的时候会调用Mouse.Capture方法来捕获鼠标。这样,在以后的鼠标事件中,Popup会判断点击是否直接发生在自己的范围内。如果不是,并且StayOpen值为False,那么Popup就会关闭。
而ComboBox也是做了类似的操作。ComboBox也是调用Mouse.Capture方法从Popup手中再次把鼠标捕获过来,这样Popup就不会自行关闭了。然后ComboBox依然会做和Popup类似的判断,并且在点击发生在其他界面元素上的时候,通过改变IsDropDownOpen的值来关闭Popup.
希望这对你有帮助。
如果这仍然不能解决你的问题,你是否能发给我们一个简单的示例。Popup控件即使在控件模板中,应该也不影响它关闭的逻辑。所以很可能是其他的一些代码,比如这个控件模板对应的控件造成了这个现象。如果你能够提供更多的信息,比如说提供一个能够充分地重现问题的小例子,可以让我们更好的帮助你找出问题所在。
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Min ZhuModerator 2011年4月7日 2:39
全部回复
-
我是这么写的,不知道对你是否有用
<UserControl x:Class="CommonControls.CCDateTime"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Width="80" Height="20" x:Name="main" >
<Canvas >
<Button Width="{Binding ElementName=main,Path=Width}" Height="{Binding ElementName=main,Path=Height}"
Name="btnDate" Background="White" BorderBrush="Black" BorderThickness="1" >
</Button>
<Popup x:Name="popUp" StaysOpen="False" Placement="Bottom"
PlacementTarget="{Binding ElementName=tbDate}" AllowsTransparency="True" >
<wf:Calendar x:Name="calendar" Background="White" >
</wf:Calendar>
</Popup></Canvas>
</UserControl> -
你好 IT农夫 ,
据我所知,Popup在打开的时候会调用Mouse.Capture方法来捕获鼠标。这样,在以后的鼠标事件中,Popup会判断点击是否直接发生在自己的范围内。如果不是,并且StayOpen值为False,那么Popup就会关闭。
而ComboBox也是做了类似的操作。ComboBox也是调用Mouse.Capture方法从Popup手中再次把鼠标捕获过来,这样Popup就不会自行关闭了。然后ComboBox依然会做和Popup类似的判断,并且在点击发生在其他界面元素上的时候,通过改变IsDropDownOpen的值来关闭Popup.
希望这对你有帮助。
如果这仍然不能解决你的问题,你是否能发给我们一个简单的示例。Popup控件即使在控件模板中,应该也不影响它关闭的逻辑。所以很可能是其他的一些代码,比如这个控件模板对应的控件造成了这个现象。如果你能够提供更多的信息,比如说提供一个能够充分地重现问题的小例子,可以让我们更好的帮助你找出问题所在。
Min Zhu [MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- 已标记为答案 Min ZhuModerator 2011年4月7日 2:39