2011-07-04 2 views
2

사용자가 요소 위에 마우스를 올려 놓았을 때 표시되는 팝업을 작성하려고합니다. 사용자가 팝업 위로 마우스를 움직이면 계속 표시됩니다. 그러나 사용자가 owner 요소와 popup 요소를 둘 다 남겨두면 popup 요소가 사라집니다. Silverlight : 마우스가 owner 요소 위에 있거나 팝업 자체 위에있을 때 팝업 표시

내가 다음을 시도했지만 작동하지 않습니다 뒤에

<UserControl x:Class="SilverlightApplication1.MainPage" 
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" 
mc:Ignorable="d" 
d:DesignHeight="300" d:DesignWidth="400"> 

<StackPanel MouseEnter="OnMouseEnter" MouseMove="OnMouseMove" MouseLeave="OnMouseLeave"> 
    <HyperlinkButton Content="Root" HorizontalAlignment="Left"/> 
    <Popup x:Name="popup" MouseEnter="OnMouseEnter" MouseMove="OnMouseMove" MouseLeave="OnMouseLeave"> 
     <StackPanel x:Name="leaves" HorizontalAlignment="Left"> 
      <HyperlinkButton Content="Leaf1" /> 
      <HyperlinkButton Content="Leaf2" /> 
     </StackPanel> 
    </Popup> 
</StackPanel> 

코드 :

public partial class MainPage : UserControl 
{ 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void OnMouseMove(object sender, MouseEventArgs e) 
    { 
     this.popup.IsOpen = true; 
    } 

    private void OnMouseEnter(object sender, MouseEventArgs e) 
    { 
     this.popup.IsOpen = true; 
    } 

    private void OnMouseLeave(object sender, MouseEventArgs e) 
    { 
     this.popup.IsOpen = false; 
    } 
} 

무엇 발생하는 소유자 요소의하는 MouseLeave (루트)는 트리거되고 팝업이 숨겨집니다.

아이디어가 있으십니까?

답변

2

사용은 DispatcherTimer 실제로이 같은 false로 IsOpen을 설정합니다 : -

public partial class MainPage: UserControl 
{ 
    DispatcherTimer popupTimer = new DispatcherTimer(); 

    public MainPage() 
    { 
     InitializeComponent(); 

     popupTimer.Interval = TimeSpan.FromMilliseconds(100); 
     popupTimer.Tick += new EventHandler(popupTimer_Tick); 
    } 

    void popupTimer_Tick(object sender, EventArgs e) 
    { 
     popupTimer.Stop(); 
     popup.IsOpen = false; 
    } 

    private void OnMouseEnter(object sender, MouseEventArgs e) 
    { 
     popupTimer.Stop(); 
     popup.IsOpen = true; 
    } 

    private void OnMouseLeave(object sender, MouseEventArgs e) 
    { 
     popupTimer.Start(); 
    } 
} 

또한 Popup 떨어져 팝업 내부의 StackPanel 위에 MouseEnter MouseMove 이벤트 이벤트를 이동합니다.

관련 문제