Skip to main content Skip to footer

How To: Display ToolTip for a Cell

Spread for Silverlight does not have an in-built feature to show tooltip for a cell. This blog provides a way to use the default Silverlight ToolTip control to show tool tip for a cell in Spread. To begin with, declare a global instance for a ToolTip control in application MainPage Class and apply the required settings in the Class Constructor.


ToolTip myTooltip = new ToolTip();  
public MainPage()  
{  
   InitializeComponent();  
   myTooltip.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;  
   myTooltip.HorizontalOffset = 10;  
   myTooltip.VerticalOffset = 10;  
   myTooltip.Width = 200;  
}  

You can set any text for Tool Tip for example, Cell text or you can show the Cell Coordinates (row and column index) or you may display any fixed string as per your requirement. In this blog we are going to show the cell’s text in tool tip. For this implementation, you need to retrieve the Position of Mouse Cursor where the tooltip has to be displayed. Along with this, you also have to get the Cell Coordinates (row and column index) at that point to retrieve the Cell Text to be populated in the Tooltip content. These information can be retrieved in SpreadSheet’s MouseMove event. In this event we can get X and Y coordinates for mouse cursor using GetPosition( ) method which takes Windows.UIElement as an argument.


var point = e.GetPosition(this.GcSpreadSheet1);  

Once we get the X and Y coordinates, get HitTest Information for GcSpreadSheet using HitTest() method with X and Y points as arguments.


var info = this.GcSpreadSheet1.HitTest(point.X, point.Y);  

Next steps is to track the mouse movement inside Spread’s Viewport and get the column and row index. After getting all these information, you can get the cell text and place it in ToolTip. The complete code implementation of the MouseMove event is shown below.


private void GcSpreadSheet1_MouseMove(object sender, MouseEventArgs e)  
{  
    var point = e.GetPosition(this.GcSpreadSheet1);  
    var info = this.GcSpreadSheet1.HitTest(point.X, point.Y);  
    myTooltip.IsOpen = false;  
    if (info.HitTestType == GrapeCity.Windows.SpreadSheet.UI.HitTestType.Viewport && info.ViewportInfo != null)  
    {  
        string cellText = GcSpreadSheet1.ActiveSheet.Cells[info.ViewportInfo.Row, info.ViewportInfo.Column].Text;  
        if (cellText != "")  
        {  
            myTooltip.Content = cellText;  
            myTooltip.IsOpen = true;  
        }  
    }  
}  

Refer to the attached samples for complete demonstration. Download Sample C# Download Sample VB

MESCIUS inc.

comments powered by Disqus