2017-01-23 103 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