2017-01-23 79 views
1

我有简单的边框控制,MouseEnter事件打开弹出控件。弹出窗口比边界更大,因此它超出了边界的范围。弹出窗口也有一些按钮和文本块,所以我需要对它进行一些控制。现在,我想要获得的东西:鼠标进入边框 - 弹出窗口出现。鼠标离开边框并弹出 - 弹出关闭。所以,当鼠标不在边框上方,而是在弹出框上时,弹出窗口保持打开状态。我不想关闭点击。你可以帮我吗? WPF中的Popup控件令我感到困惑。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; 
    } 
} 
+0

你会在这里分享你的一些代码吗? – MikkaRin

+0

只需为'IsMouseOver'属性的'popup'设置触发器,如果​​IsMouseOver == true,那么在setter' popup.IsOpen = true'中,然后设置'IsMouseOver == false' =>'popup.IsOpen = false' 。它非常简单。 – Shakra

回答

0

代码隐藏:

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事件处理程序,并检查每个鼠标是否在关闭弹出窗口之前。

+0

这是解决方案!谢谢! – Paweu