在WPF中鼠标悬停弹出时如何使弹出窗口保持打开状态?

时间:2015-10-08 11:45:28

标签: c# wpf popup label

我正在制作一个WPF应用程序,我有一个标签。当鼠标悬停弹出窗口上的标签触发器出现时。在这个弹出窗口中,我有一些带有不同动作的按钮。但是当我鼠标悬停在标签上时,弹出窗口出现但我无法通过触发器mouseleave消失之前使用弹出窗口。

这是我的XAML代码:

    <Label x:Name="userLabel" Content="Label" Grid.Column="2" HorizontalAlignment="Left" Margin="404,31,0,0" VerticalAlignment="Top" Width="145" Foreground="White" MouseEnter="UserLabelMouseEnter" MouseLeave="UserLabelMouseLeave"/>

    <Popup Name="UserMenuPopUp" PopupAnimation="Fade" Height="auto" Margin="0,0,0,0" AllowsTransparency="True" Placement="bottom" PlacementTarget="{Binding ElementName=userLabel}" StaysOpen="false">
        <Border BorderThickness="1" Background="#EEEEEE" Height="160" HorizontalAlignment="Left" Width="195" RenderTransformOrigin="0.5,0.5">
            <Grid>
                <Button x:Name="profile" Content="profile" Margin="0,0,0,0" VerticalAlignment="Top" Height="40" BorderThickness="0" Click="ProfileBtn_Click" Foreground="White" HorizontalContentAlignment="Right">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Background" Value="#282828"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#79B539"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <Button x:Name="RentalPlans" Content="Rental plans" Margin="0,40,0,0" VerticalAlignment="Top" Height="40" BorderThickness="0" Click="RentalPlansBtn_Click" Foreground="White">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Background" Value="#282828"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#79B539"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <Button x:Name="MyCredits" Content="My credit cards" Margin="0,80,0,0" VerticalAlignment="Top" Height="40" BorderThickness="0" Click="MycreditsBtn_Click" Foreground="White">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Background" Value="#282828"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#79B539"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
                <Button x:Name="LogOut" Content="Log out" Margin="0,120,0,0" VerticalAlignment="Top" Height="40" BorderThickness="0" Foreground="White" Click="LogOuBtn_Click">
                    <Button.Style>
                        <Style TargetType="{x:Type Button}">
                            <Setter Property="Background" Value="#282828"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="{x:Type Button}">
                                        <Border Background="{TemplateBinding Background}">
                                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="Background" Value="#79B539"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>

            </Grid>
        </Border>
    </Popup>

这是我的C#代码:

    private void UserLabelMouseEnter(object sender, MouseEventArgs e)
    {
        UserMenuPopUp.IsOpen = true;
    }

    private void UserLabelMouseLeave(object sender, MouseEventArgs e)
    {
        UserMenuPopUp.IsOpen = false;

    }

1 个答案:

答案 0 :(得分:0)

您可以将UserLabelMouseLeave代码移至Popup的MouseLeave事件。