如何在wpf中关注焦点之前保持弹出状态

时间:2013-09-11 09:13:07

标签: wpf xaml popup controltemplate eventtrigger

我正在关注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>

2 个答案:

答案 0 :(得分:0)

您必须为弹出窗口添加mouseenter和mouseleave事件,也与textbox

相同
                  <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>

答案 1 :(得分:0)

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

<Popup IsOpen="{Binding IsPopupOpen}" StaysOpen="False" AllowsTransparency="True">
相关问题