How to make a popup stays open when mouseover popup in WPF?

邮差的信 提交于 2019-12-23 04:43:46

问题


I'm making an WPF application where I have a label. When label trigger on mouseover popup should appear. In this popup I have some buttons with differents action. But when I mouseover the label the popup appear but I can't use my popup before it disappear by trigger mouseleave.

Here is my XAML code:

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

Here is my C# code:

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

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

    }

回答1:


You can move the UserLabelMouseLeave code to the Popup's MouseLeave event.



来源:https://stackoverflow.com/questions/33014833/how-to-make-a-popup-stays-open-when-mouseover-popup-in-wpf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!