Tuesday, September 4, 2012

Assign ToolTip to dynamically created colum of silverlight Gridview and How to create style for element runtime

Shout it kick it on DotNetKicks.com
In this post I am going to discuss about displaying tooptip for autogenerated columns of toolkit silverlight and telerik silverlight gridcongtrol. Also goint to discuss about how to create style dynamically for the silverlight control.

How to create style in codebehind i.e. runtime for control To define the style sheet for any silverlight control on runtime there you need to do following things

1. Create Style object with the type of control
var style = new Style(typeof(controlType)); 
Control type can be Grid, GridviewCell, Textblock etc. i.e any valid silverlight control.
2. Add Setter to Created Style Object
style.Setters.Add(new Setter(StylePropertyName, Value));
You need to pass property name and value now after this, below discussion you will find example for the same
3. Attach Created Style to the control type object
controlboject.stylefor = style; 
Now following describe how to user the dynamic style and display tooltip for header of autogenerated columns of gridview.

To apply the style at runtime to autogetnerated column I made use of AutoGenerated Event of the grid in both of the below example.
Code for displaying tootip for Telerik grid
private void EditableGV_AutoGeneratingColumn
       (object sender, GridViewAutoGeneratingColumnEventArgs e)
   GridViewDataColumn column = e.Column as GridViewDataColumn;

   //for tooltip dispaly
   var style = new Style(typeof(GridViewHeaderCell));
   style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, 
   column.HeaderCellStyle = style; 
As you see in above code Style is get created for the Headercell of grid view. ToolTip property style is added by using setting as we discuss above. Lastly assigned created style to headercell style.

Silverlight gridview
Same as Telerik gird,in tool grid I made use of Autogenerated column
private void dataGrid1_AutoGeneratingColumn
            (object sender, DataGridAutoGeneratingColumnEventArgs e)
  DataGridBoundColumn column = e.Column as DataGridBoundColumn;
  var style = new Style(typeof(DataGridColumnHeader));
  style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, 
  column.HeaderStyle = style; 
Just few code difference , here style is created for headercell only but the name of the class is diffrerent, which you can notice easily by comparing both code.


  1. Hi,
    Using the below code i m unable to set maxwidth of tooltip. Can u please help in setting maxwidth property in C#.
    var style = new Style(typeof(DataGridColumnHeader));
    var gridTooltip = "Grid Tootip";
    style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, new ToolTip { MaxWidth = 300, Content = gridTooltip }.Content));
    column.HeaderStyle = style;

  2. can u help me when mouseover the imagebutton how to bind data in tooltip??

  3. I have grid which generates dynamic columns and by using this suggestion I got my results.
    Thanks a lot.

  4. My family all the time say that I am killing my time here
    at web, but I know I am getting experience everyday by reading such pleasant posts.

    Review my blog :: heboostblog.com (http://hypnosisseries.com/article/article.php?id=6742)

  5. It's going to be finish of mine day, except before finish I am reading this great
    piece of writing to increase my know-how.

    My homepage - Family Guy The Quest for Stuff hacks (http://www.highproductivity.net/)

  6. We're a bunch of volunteers and starting a brand new scheme in our community.
    Your web site provided us with helpful information to work on. You have performed an impressive job and our whole community shall be grateful
    to you.

    Feel free to surf to my blog - emetophobie hypnose

  7. We're a group of volunteers and starting a new
    scheme in our community. Your web site provided us with valuable information to work on. You have
    done an impressive job and our entire community will be grateful to you.

    My blog post After Effects Rain

  8. What i don't understood is if truth be told how you are now not actually
    much more neatly-preferred than you might be now.
    You are very intelligent. You recognize therefore considerably in the case of this topic,
    made me in my opinion imagine it from a lot of various angles.
    Its like women and men are not interested except it is something to do with Girl gaga!
    Your personal stuffs nice. At all times care for it up!

    Here is my site: rings designs

  9. I am now not cеrtain the place you are getting уour information, but good toƿic.
    I needs to spend a while learning muсh more οг working out more.
    Ҭhanks for fantastic info I used to be looking for this info for my mission.

    my site - online mafia game