如何保持弹出窗口打开,直到它在wpf中具有焦点
我有以下XAML用于显示文本框的鼠标输入弹出事件和关闭文本框的鼠标离开弹出事件。所以,当我尝试进入弹出窗口时,鼠标离开事件被调用,弹出窗口关闭。因此,如果我正在关注弹出窗口,我想要的弹出窗口不应该关闭,而如果我点击了弹出窗口,或者鼠标没有在弹出窗口上方,弹出窗口应该关闭 注意:但若我并没有专注于弹出窗口,那个么弹出窗口应该在鼠标离开时关闭如何保持弹出窗口打开,直到它在wpf中具有焦点,wpf,xaml,popup,controltemplate,eventtrigger,Wpf,Xaml,Popup,Controltemplate,Eventtrigger,我有以下XAML用于显示文本框的鼠标输入弹出事件和关闭文本框的鼠标离开弹出事件。所以,当我尝试进入弹出窗口时,鼠标离开事件被调用,弹出窗口关闭。因此,如果我正在关注弹出窗口,我想要的弹出窗口不应该关闭,而如果我点击了弹出窗口,或者鼠标没有在弹出窗口上方,弹出窗口应该关闭 注意:但若我并没有专注于弹出窗口,那个么弹出窗口应该在鼠标离开时关闭 <Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.m
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Control VerticalAlignment="Top">
<Control.Template>
<ControlTemplate>
<StackPanel>
<TextBox x:Name="MyText"></TextBox>
<Popup x:Name="Popup" PopupAnimation="Fade" VerticalAlignment="Top">
<Border Background="Red">
<TextBlock>Test Popup Content</TextBlock>
</Border>
</Popup>
</StackPanel>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="MyText">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave" SourceName="MyText">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Control.Template>
</Control>
</Grid>
</Window>
测试弹出内容
您必须为弹出窗口添加mouseenter和mouseleave事件,这与文本框相同
<EventTrigger RoutedEvent="UIElement.MouseEnter" SourceName="Popup">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="UIElement.MouseLeave" SourceName="Popup">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="Popup" Storyboard.TargetProperty="(Popup.IsOpen)">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False"/>
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
我这样做的方法是将
bool
值绑定到Popup.IsOpen
属性。您仍然可以在MouseEnter
处理程序中将其设置为true,但您可以选择何时将其设置为false:
<Popup IsOpen="{Binding IsPopupOpen}" StaysOpen="False" AllowsTransparency="True">
请不要使用所有粗体文本。谢谢您的回答,但实际上我不想使用ViewModel的属性,而是想通过XAML本身实现所有的神奇功能