|
1 | | -# How-to-show-the-rendered-HTML-inside-the-ToolTip-of-the-WPF-SfDataGrid |
2 | | -This demo shows how to show the rendered HTML inside the ToolTip of the WPF SfDataGrid |
| 1 | +# How to show the rendered HTML inside the ToolTip of the WPF DataGrid (SfDataGrid) |
| 2 | + |
| 3 | +By default, [WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid) does not have built-in support to render HTML code and display it in the **ToolTip**. However, we can achieve this requirement by rendering the **HTML** using [WPF RichTextBoxAdv](https://www.syncfusion.com/wpf-controls/richtextbox) (SfRichTextBoxAdv). |
| 4 | + |
| 5 | +In this approach, load the HTML code into RichTextBoxAdv, render the HTML content, and then set RichTextBoxAdv as the ToolTip content inside the [CellToolTipOpening](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.SfDataGrid.html#Syncfusion_UI_Xaml_Grid_SfDataGrid_CellToolTipOpening) event. |
| 6 | + |
| 7 | +**Step 1: Subscribe to the CellToolTipOpening Event of the DataGrid** |
| 8 | + |
| 9 | +Subscribe to the CellToolTipOpening event of DataGrid, and enable the [ShowToolTip](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.SfGridBase.html#Syncfusion_UI_Xaml_Grid_SfGridBase_ShowToolTip) property for the desired column where the ToolTip should be displayed. |
| 10 | + |
| 11 | + ```xml |
| 12 | +<syncfusion:SfDataGrid HorizontalAlignment="Center" |
| 13 | + x:Name="dataGrid" |
| 14 | + ItemsSource="{Binding Orders}" |
| 15 | + AutoGenerateColumns="False" |
| 16 | + CellToolTipOpening="OnCellToolTipOpening"> |
| 17 | + <syncfusion:SfDataGrid.Columns> |
| 18 | + <syncfusion:GridTextColumn HeaderText="Customer ID" |
| 19 | + MappingName="CustomerID" |
| 20 | + ShowToolTip="True"/> |
| 21 | + <syncfusion:GridTextColumn HeaderText="Customer Name" |
| 22 | + MappingName="CustomerName"/> |
| 23 | + </syncfusion:SfDataGrid.Columns> |
| 24 | +</syncfusion:SfDataGrid> |
| 25 | + ``` |
| 26 | +**Step 2: Customize the ToolTip content to display as HTML content** |
| 27 | + |
| 28 | + In the event handler, retrieve the HTML code, convert it into a stream, and pass it to RichTextBoxAdv. Then, set RichTextBoxAdv as the ToolTip content of the DataGrid. |
| 29 | + |
| 30 | + ```csharp |
| 31 | + SfRichTextBoxAdv richTextBoxAdv = new SfRichTextBoxAdv() { Width = 300, Height = 200, LayoutType = LayoutType.Continuous}; |
| 32 | + |
| 33 | + private void OnCellToolTipOpening(object sender, GridCellToolTipOpeningEventArgs e) |
| 34 | + { |
| 35 | + if (e.Record != null && e.Record is OrderInfo) |
| 36 | + { |
| 37 | + string htmlContent = (e.Record as OrderInfo).HTMLCode; |
| 38 | + if (htmlContent != null) |
| 39 | + { |
| 40 | + Stream stream = new MemoryStream(); |
| 41 | + byte[] bytes = Encoding.UTF8.GetBytes(htmlContent); |
| 42 | + if (bytes != null) |
| 43 | + stream.Write(bytes, 0, bytes.Length); |
| 44 | + stream.Position = 0; |
| 45 | + richTextBoxAdv.Load(stream, FormatType.Html); |
| 46 | + e.ToolTip.Content = richTextBoxAdv; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + ``` |
| 51 | + |
| 52 | +  |
| 53 | + |
| 54 | +Take a moment to peruse the [WPF DataGrid - ToolTip](https://help.syncfusion.com/wpf/datagrid/tooltip) documentation where you can find about the ToolTip with code examples. |
0 commit comments