Category / Section
How to prevent the opening of an empty ToolTip when the GridCell is empty?
1 min read
You can prevent the opening of an empty Tooltip by handling the loaded event of the content loading into the Tooltip template.
Step 1: Consider the TextBlock as the content of the Tooltip. In the following code example, the loaded event of the TextBlock is handled and the parent, Tooltip, is found by using the FindAncestor method. When the TextBlock.Text is empty, then set the ToolTip.IsOpen to false to prevent opening an empty Tooltip.
using Microsoft.VisualStudio.PlatformUI; public class TextBlockExt : TextBlock { public TextBlockExt() { this.Loaded += TextBlockExt_Loaded; } void TextBlockExt_Loaded(object sender, RoutedEventArgs e) { if (!string.IsNullOrEmpty(this.Text)) return; var tooltip = this.FindAncestor<ToolTip>(); if (IsVisible) tooltip.IsOpen = false; } }
Step 2: The customized TextBlock is loaded into the Tooltip template of the GridColumn.ToolTipTemplate to achieve the desired result.
<syncfusion:SfDataGrid.Columns> <syncfusion:GridTextColumn ShowToolTip="True" MappingName="EmployeeName" > <syncfusion:GridTextColumn.ToolTipTemplate> <DataTemplate> <local:TextBlockExt Text="{Binding Path=EmployeeName}" /> </DataTemplate> </syncfusion:GridTextColumn.ToolTipTemplate> </syncfusion:GridTextColumn> </syncfusion:SfDataGrid.Columns>
Note: You have to set the GridColumn.ShowToolTip to enable ToolTip support as given in the above code.