0

我正在关注 XAML,用于在文本框的鼠标输入事件上显示弹出窗口,并在文本框的鼠标离开事件上关闭弹出窗口。因此,当我尝试转到弹出窗口时,会调用鼠标离开事件并关闭弹出窗口。因此,如果我专注于弹出窗口,则不应关闭我想要的弹出窗口,而如果我单击了弹出窗口或鼠标未弹出,则应关闭弹出窗口。

注意:但是如果我没有专注于弹出窗口,则应在鼠标离开时关闭弹出窗口。

<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>
4

2 回答 2

0

我这样做的方法是将bool值绑定到Popup.IsOpen属性。您仍然可以在MouseEnter处理程序中将其设置为 true,但您可以选择何时将其设置为 false:

<Popup IsOpen="{Binding IsPopupOpen}" StaysOpen="False" AllowsTransparency="True">
于 2013-09-11T09:23:26.553 回答
0

您必须为弹出窗口添加 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>
于 2013-09-11T09:20:29.023 回答