I seem to have a flickering issue with a popup that shows up on hover. When the popup is open and the mouse goes over the popup, the popup flickers because its constantly trying to figure out which element its over and what data to pull. The problem is fixed when I remove the unhover event, but then when the mouse is off the map the popup is still open. Is there anyway to prevent the flickering and keep the unhover? I've been trying to figure out a way to set a variable that has a value when the mouse is over an element but that is null when its just over the background. Any ideas?
This would be one way of approaching it:
public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); _popup = thePopup; _popup.Child.MouseEnter += new MouseEventHandler(Child_MouseEnter); _popup.Child.MouseLeave += new MouseEventHandler(Child_MouseLeave); } void Child_MouseLeave(object sender, MouseEventArgs e) { _mouseOverPopup = false; PossiblyHidePopup(); } void Child_MouseEnter(object sender, MouseEventArgs e) { _mouseOverPopup = true; } private Popup _popup; private bool _mouseOverPopup; private MapElement _hoveredElement; private void XamWebMap_ElementHover( object sender, Infragistics.Silverlight.Map.MapElementHoverEventArgs e) { _hoveredElement = e.Element; TransitionPopup(); } private void PossiblyHidePopup() { //add a low priority item to the //queue to check if popup should be closed. Dispatcher.BeginInvoke(TransitionPopup); } private void XamWebMap_ElementUnhover( object sender, Infragistics.Silverlight.Map.MapElementHoverEventArgs e) { _hoveredElement = null; PossiblyHidePopup(); } private void TransitionPopup() { if (!_mouseOverPopup && _hoveredElement == null) { _popup.IsOpen = false; _popup.DataContext = null; } else if (_hoveredElement != null && _popup.DataContext != _hoveredElement) { Point pos = _hoveredElement.ActualSymbolOrigin; pos = theMap.Viewport.RootCanvas .RenderTransform.Transform(pos); _popup.HorizontalOffset = pos.X; _popup.VerticalOffset = pos.Y; _popup.IsOpen = true; _popup.DataContext = _hoveredElement; } } }
-Graham
see updated previous post, removed unused code.
Try:
AddHandler _popup.Child.MouseEnter, AddressOf Child_MouseEnter AddHandler _popup.Child.MouseLeave, AddressOf Child_MouseLeave
And:
ElseIf _hoveredElement IsNot Nothing AndAlso _popup.DataContext IsNot _hoveredElement Then
This was verified using the Graham VB.NET compiler, so let me know if there are any errors.
Thanks Graham. My project is done in vb.net (unfortunately) and after using a converter this is what it looks like.
Public Partial Class MainPage Inherits UserControl Public Sub New() InitializeComponent() _popup = thePopup _popup.Child.MouseEnter += New MouseEventHandler(AddressOf Child_MouseEnter) _popup.Child.MouseLeave += New MouseEventHandler(AddressOf Child_MouseLeave) End Sub Private Sub Child_MouseLeave(sender As Object, e As MouseEventArgs) _mouseOverPopup = False PossiblyHidePopup() End Sub Private Sub Child_MouseEnter(sender As Object, e As MouseEventArgs) _mouseOverPopup = True End Sub Private _popup As Popup Private _mouseOverPopup As Boolean Private _hoveredElement As MapElement Private Sub XamWebMap_ElementHover(sender As Object, e As Infragistics.Silverlight.Map.MapElementHoverEventArgs) _hoveredElement = e.Element TransitionPopup() End Sub Private Sub PossiblyHidePopup() 'add a low priority item to the 'queue to check if popup should be closed. Dispatcher.BeginInvoke(AddressOf TransitionPopup) End Sub Private Sub XamWebMap_ElementUnhover(sender As Object, e As Infragistics.Silverlight.Map.MapElementHoverEventArgs) _hoveredElement = Nothing PossiblyHidePopup() End Sub Private Sub TransitionPopup() If Not _mouseOverPopup AndAlso _hoveredElement Is Nothing Then _popup.IsOpen = False _popup.DataContext = Nothing ElseIf _hoveredElement IsNot Nothing AndAlso _popup.DataContext <> _hoveredElement Then Dim pos As Point = _hoveredElement.ActualSymbolOrigin pos = theMap.Viewport.RootCanvas.RenderTransform.Transform(pos) _popup.HorizontalOffset = pos.X _popup.VerticalOffset = pos.Y _popup.IsOpen = True _popup.DataContext = _hoveredElement End If End SubEnd Class
Silverlight seems to have problems with 2 sections of this code. These two lines:
_popup.Child.MouseEnter += New MouseEventHandler(AddressOf Child_MouseEnter)_popup.Child.MouseLeave += New MouseEventHandler(AddressOf Child_MouseLeave)
The error says "Public Event MouseEnter(sender As Object, e As System.Windows.Input.MouseEventArgs) is an event and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."
The other error is here:
ElseIf _hoveredElement IsNot Nothing AndAlso _popup.DataContext <> _hoveredElement Then...
The error says "Operator '<>' is not defined for types 'Object' and 'Infragistics.Silverlight.Map.MapElement'"
Do you have any suggestions?
Thanks!