This is a migrated thread and some comments may be shown as answers.

GridViewImageColumn & ToolTipTemplateSelector

9 Answers 429 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gaga
Top achievements
Rank 2
Gaga asked on 07 Feb 2011, 05:24 PM
Hi,
I have a little problem with writing my DataTemplateSelector, so I thought that maybe someone already had this problem and could help.

I have a GridViewImageColumn that displays status icon (if item has some status set). I want to display some comment in tooltip, if icon exists.
I tried to solve this problem with creating my DataTemplateSelector and assigning it to ToolTipTemplateSelector property of GridViewImageColumn .
public override DataTemplate SelectTemplate(object item, DependencyObject container)
        
            var template = base.SelectTemplate(item, container); 
            string iconTooltip = string.Empty; 
            /* Here goes some code that get value of iconTooltip */
  
            if (String.IsNullOrEmpty(iconTooltip)) 
            {
                template = null;
            
            else 
            {
                template = (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><TextBlock Text=""{Binding iconTooltip}"" /></DataTemplate>");
               
            return template; 
        }

My problem is that in this case: (String.IsNullOrEmpty(iconTooltip)) tooltip is still visible, but empty (screenshot attached). I thought that setting template to null would give him signal not to show tooltip but I was wrong (obviously).

Does someone had an idea how to solve this?

9 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 10 Feb 2011, 03:22 PM
Hi Gaga,

This is the expected behavior since no matter that you are setting it to null, the ToolTip is still instantiated. In order to overcome this issue, you have a couple of options. A possible approach is to create a CellTemplateSelector instead and define the ToolTip property for those cells that you require to have a ToolTip. The other possibility is to define the CellTemplate for that column and set it ToolTip accordingly. 

Best wishes,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Gaga
Top achievements
Rank 2
answered on 10 Feb 2011, 04:42 PM
Hi Maya,

Thank for your help. I created a CellTemplateSelector as you sugested and now it works fine. :)

I noticed something strange though, if I set CellTemplateSelector for GridViewImageColumn it does not work, but if I set the same CellTemplateSelector for ordinary GridViewDataColumn it is behaving as expected. Is this a bug or expected behavior?

Example of my custom CellTemplateSelector :
public class ImageTooltipDataTemplateSelector : DataTemplateSelector
{
    public ImageTooltipDataTemplateSelector()
    {}
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    
        string iconURL= string.Empty;
        /* get value for imageURL*/
        if (String.IsNullOrEmpty(iconURL)) 
        {
            return (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""></DataTemplate>");
        
        else 
        {
            return (DataTemplate)XamlReader.Load(@"<DataTemplate xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""><Image Source=""{Binding iconURL}"" Height=""16"" Width=""16"" ToolTipService.ToolTip=""{Binding iconTooltip}"" /></DataTemplate>");   
        
    }
}

Best Wishes,
Gaga
0
Maya
Telerik team
answered on 16 Feb 2011, 10:21 AM
Hi Gaga,

I am glad your issue got resolved. As far as the application of the CellTemplateSelector on a GridViewImageColumn is concerned, we would need a bit more time for investigating it. I will get back to you once we have an appropriate solution.

Kind regards,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
Tim
Top achievements
Rank 1
answered on 12 Aug 2011, 10:44 PM
Hi Maya,

I noticed the same issue as Gaga, but I do not see how this is a valid approach because it seems incomplete.  The solution above places a tooltip on the content in the cell, not the actual cell.  Therefore, the tooltip will not show if you hover over an empty part of the cell where there is no content.  How do I selectively show a tooltip for the entire cell (not the content)?

ToolTipTemplate and ToolTipTemplateSelector are applied to the entire cell.  But once the template or selector is defined, there seems to be no way to turn it off.  There is no direct visibility setting and selecting a null or empty template (to try to hide it) shows a small empty tooltip that the users do not want to see (as Gaga pointed out).  Can you help me turn the tooltip on and off for the entire cell (not apply it to child elements)?

We are using 2011.2.712.1040.

Thanks!
0
Maya
Telerik team
answered on 15 Aug 2011, 12:39 PM
Hi Tim,

I have tested the scenario, but still the ToolTip defined through a CellTemplateSelector is applied property to the whole element (in my case TextBlock) in the data template, not just the content. I am sending you the sample project I used for the test. Please take a look at it and let me know if there are any misunderstandings. 

Best wishes,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tim
Top achievements
Rank 1
answered on 15 Aug 2011, 06:02 PM
Maya,

Thank you for the quick response!  You are correct (my testing was not).  The default behavior of the cell content is to expand to fill the cell, which means that I don't need additional xaml to do that.  (good!) 

However, this approach requires every column to use two dedicated datatemplates (one with a tooltip and one without) and a re-usable celltemplateselector (to choose between the two datatemplates) just to turn off a tooltip.  One column is fine, but when you have a lot of columns sharing a single tooltip, this requires a lot of repetitious xaml.

I seem to be having some success by assigning a ToolTipTemplateSelector in xaml and then selectively setting the ToolTipTemplateSelector property (not the ToolTipTemplate) to null, programmatically, to turn off the tooltip.  This seems to stop the empty tooltip stubs from showing up.  So I may pursue this route instead since it is a less code (when you have lots of columns).  Any thoughts on this?

In the future, it would be nice if tooltip visibility was simply a bind-able property.

Thanks for the help.

0
Maya
Telerik team
answered on 18 Aug 2011, 07:23 AM
Hello Tim,

Indeed, you are quite correct - following the idea of creating a CellTemplateSelector when you have a lot of column will not be a small task to do. So, if you find how to use the ToolTip template selector so that it meets your specific requirements, it would be the way to go. 
In case you find it appropriate, it would be great if you share your approach with the community, thus helping the others who might have a similar problem.
 

Best wishes,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

0
Tim
Top achievements
Rank 1
answered on 18 Aug 2011, 05:13 PM

If anyone sees a better way or if you see any holes in this workaround, please share.  Thanks! 

For us, the least amount of code involves these 3 steps and works for all cells/columns that show that tooltip without requiring modification of the cell's content or templates.

The basic idea is not try to show the tooltip selectively, but rather to show it all the time and hide it when you don't want it.

1.  Use ToolTipTemplate as if you want to show the tooltip all the time.  Put it on each column that you want to show it on.

<telerik:GridViewDataColumn DataMemberBinding="{Binding Whatever}" Header="Whatever" 
                            ToolTipTemplate="{StaticResource MyToolTipTemplate}"  />

2.  RadGridView fires "CellLoaded" after it draws/re-draws each cell.  When it draws a cell, it uses the column's ToolTipTemplate as the content for the ToolTipService.ToolTip and applies it to the cell.  Use the grid's CellLoaded event to hide this tooltip when it is not appropariate to show it.  This event will fire whenever the grid draws a cell (for instance if it is virtualized and scrolls into view).  Note: this will NOT affect cells that are ALREADY drawn.

private void myRadGridView_CellLoaded(object sender, CellEventArgs e) {
   if (e.Cell is GridViewCell) 
   {
      var col = e.Cell.DataColumn;
      DataTemplate myToolTipTemplate = (DataTemplate)this.Resources["MyToolTipTemplate"];
      //make sure we only affect this tooltiptemplate and don't turn off others
      if (myToolTipTemplate.Equals(col.ToolTipTemplate)) 
      {
         //this custom condition determines if tooltip should show, if not, hide it
         if (!this.ShowToolTip)
            ToolTipService.SetToolTip(e.Cell, null);
      }
   }
}

3. What about cells that are already drawn (i.e. visible)?  Force them to redraw.  When the tooltip visibility condition changes, this custom method forces visible cells to redraw by resetting the CellTemplateSelector for each column.  This causes the event in #2 to fire for visible cells.  This has no effect on non-visible columns/cells (assuming virtualization is on).

private static void ShowToolTip_Changed() 
{
   foreach (var column in myRadGridView.Columns) {
    DataTemplateSelector original = column.CellTemplateSelector;    //ok if null 
    column.CellTemplateSelector = new EmptyCellTemplateSelector();  //dummy selector 
    column.CellTemplateSelector = original;
   }
}

0
Maya
Telerik team
answered on 19 Aug 2011, 07:03 AM
Hello Tim,

Thank you a lot for sharing the approach you found with the community.
I have updated your Telerik points. 

 

Greetings,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
GridView
Asked by
Gaga
Top achievements
Rank 2
Answers by
Maya
Telerik team
Gaga
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Share this question
or