【问题标题】:WPF Popup - hide only on mouse leave the control and the popupWPF Popup - 仅在鼠标离开控件和弹出窗口时隐藏
【发布时间】:2017-01-23 21:06:10
【问题描述】:

我有一个简单的边界控制,带有MouseEnter 事件,它打开了弹出控件。弹出窗口比边界大,所以它超出了边界的范围。弹出窗口还有一些按钮和文本块,所以我需要对其进行一些控制。现在,我想获得什么:鼠标进入边界 - 弹出窗口出现。鼠标离开边界和弹出窗口 - 然后弹出窗口关闭。因此,当鼠标不在边界上而是在弹出窗口上时 - 弹出窗口保持打开状态。我不想在点击时关闭。你能帮助我吗? WPF 中的弹出控件让我感到困惑。

编辑: 对了,代码:

<Window x:Class="Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Sandbox"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid Margin="20">
    <Popup PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=OpenPopupBorder}" AllowsTransparency="True" StaysOpen="True" x:Name="DeviceStatusSnippetPopup">
        <Border Background="White" Padding="5" BorderBrush="Black" BorderThickness="1">
            <Grid>
                <StackPanel Orientation="Vertical">
                    <TextBlock HorizontalAlignment="Center" Text="Device status snippet" Margin="0 10 0 10"/>
                    <StackPanel Orientation="Horizontal">
                        <Button Style="{StaticResource btn-default}" Margin="0 0 5 0" Content="Go to device"/>
                        <Button Style="{StaticResource btn-default}" Content="Refresh device"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </Border>
    </Popup>
    <Border x:Name="OpenPopupBorder" Width="90" Height="30" Background="LightGray" MouseEnter="Border_MouseEnter"/>
</Grid>

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

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

【问题讨论】:

  • 你会在这里分享你的一些代码吗?
  • 只需为IsMouseOver 属性的popup 设置触发器,如果​​IsMouseOver== true 然后在setter popup.IsOpen=true 中,第二个IsMouseOver==false => popup.IsOpen=false。真的很简单。

标签: c# wpf xaml


【解决方案1】:

代码隐藏:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

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

    private void Popup_MouseLeave(object sender, MouseEventArgs e)
    {
        if (!OpenPopupBorder.IsMouseOver)
            DeviceStatusSnippetPopup.IsOpen = false;
    }


    private void Border_MouseLeave(object sender, MouseEventArgs e)
    {
        if (!DeviceStatusSnippetPopup.IsMouseOver)
            DeviceStatusSnippetPopup.IsOpen = false;
    }
}

XAML(减去窗口):

<Grid Margin="20">
    <Popup MouseLeave="Popup_MouseLeave" PopupAnimation="Fade" Placement="Top" PlacementTarget="{Binding ElementName=OpenPopupBorder}" AllowsTransparency="True" StaysOpen="True" x:Name="DeviceStatusSnippetPopup">
        <Border Background="White" Padding="5" BorderBrush="Black" BorderThickness="1">
            <Grid>
                <StackPanel Orientation="Vertical">
                    <TextBlock HorizontalAlignment="Center" Text="Device status snippet" Margin="0 10 0 10"/>
                    <StackPanel Orientation="Horizontal">
                        <Button Margin="0 0 5 0" Content="Go to device"/>
                        <Button Content="Refresh device"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </Border>
    </Popup>
    <Border x:Name="OpenPopupBorder" Width="90" Height="30" Background="LightGray" MouseEnter="Border_MouseEnter" MouseLeave="Border_MouseLeave"/>
</Grid>

基本上,您为边框和弹出窗口都实现了 MouseLeave 事件处理程序,并在关闭弹出窗口之前检查鼠标是否在另一个上方。

【讨论】:

  • 这就是解决方案!谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
相关资源
最近更新 更多