Category / Section
How to set the show duration and show delay duration for tooltip in WPF DataGrid (SfDataGrid)?
3 mins read
You can set TimeDuration and TimeDelay for ToolTip using ToolTipService.SetShowDuration and ToolTipService.SetInitialShowDelay methods in WPF DataGrid (SfDataGrid).
C#
ToolTip toolTip = new ToolTip(); toolTip.PlacementTarget = datagrid; // ToolTip assigned for datagrid datagrid.ToolTip = toolTip; toolTip.Content = "sync"; ToolTipService.SetShowDuration(datagrid, 7000); // Initial TimeDelay ToolTipService.SetInitialShowDelay(datagrid, 20000000); // Set Position for ToolTip ToolTipService.SetPlacement(datagrid, System.Windows.Controls.Primitives.PlacementMode.Mouse);
But when you are using the ToolTip manually in code behind these properties settings won’t take in consideration. Where you have to set time delays manually using DispatcherTimer like below code example.
C#
this.datagrid.MouseMove += datagrid_MouseMove; int previourerecordindex = -1; int previouscolindex = -1; Point previouspoint; DispatcherTimer timer = new DispatcherTimer() { Interval = new TimeSpan(20000000) }; DispatcherTimer closetimer = new DispatcherTimer() { Interval = new TimeSpan(0, 0, 3) }; ToolTip toolTip = new ToolTip(); EventHandler handler; EventHandler closetimerhandler; void datagrid_MouseMove(object sender, MouseEventArgs e) { VisualContainer visualcontainer = (VisualContainer)datagrid.GetPropertyValue("VisualContainer"); var point = e.GetPosition(visualcontainer); var rowcolumnindex = visualcontainer.PointToCellRowColumnIndex(point); var rowindex = rowcolumnindex.RowIndex; var columnindex = rowcolumnindex.ColumnIndex; // Get the resolved current record index int recordIndex = this.datagrid.ResolveToRecordIndex(rowindex); if (recordIndex < 0 || columnindex < 0) { timer.Stop(); closetimer.Stop(); toolTip.IsOpen = false; return; } if (recordIndex == previourerecordindex && columnindex == previouscolindex) return; timer.Stop(); closetimer.Stop(); toolTip.IsOpen = false; previourerecordindex = recordIndex; previouscolindex = columnindex; previouspoint = point; if (handler == null) { handler = (s, arg) => { timer.Stop(); // Get the current row record var mappingName = this.datagrid.Columns[previouscolindex].MappingName; var record1 = this.datagrid.View.Records.GetItemAt(previourerecordindex); if (record1 != null) { var cellvalue = record1.GetType().GetProperty(mappingName).GetValue(record1).ToString(); toolTip.Content = cellvalue; toolTip.IsOpen = true; } closetimer.Start(); }; } timer.Tick -= handler; timer.Tick += handler; if (closetimerhandler == null) { closetimerhandler = (s, arg) => { closetimer.Stop(); if (toolTip.IsOpen) toolTip.IsOpen = false; }; } closetimer.Tick -= closetimerhandler; closetimer.Tick += closetimerhandler; timer.Start(); }