How can I bind a columns tooltip to a data annotation?

3 posts, 0 answers
  1. John
    John avatar
    6 posts
    Member since:
    Mar 2009

    Posted 14 Jul 2014 Link to this post

    We are binding the grid to a collection of items that use data annotation to drive the column headers.  Is there a way to automatically bind the tooltip to a particular attribute (or any attribute for that matter)?

    Here is our grid:
    <telerik:RadGridView HorizontalAlignment="Stretch"
                     VerticalAlignment="Stretch"
                     ShowGroupPanel="False"
                     IsFilteringAllowed="False"
                     ItemsSource="{Binding Instances}"
                     AutoGenerateColumns="True" />


    This is a sample property:
    [Display(ResourceType = typeof(Resources), Name = "InstanceViewModel_Name_Name")]
    public string Name
    {
        get { return InstanceModel.Name; }
        set
        {
            if (InstanceModel != null)
            {
                InstanceModel.Name = value;
                RaisePropertyChanged(() => Name);
            }
        }
    }

    Ideally we'd like something similar to (where Description appears as the tooltip for the column):
    [Display(ResourceType = typeof(Resources), Name = "InstanceViewModel_Name_Name", Description = "InstanceViewModel_Name_Description")]
    public string Name
    {
        ...
    }

    Hopefully there is a solution
    Thanks

    John
  2. John
    John avatar
    6 posts
    Member since:
    Mar 2009

    Posted 15 Jul 2014 in reply to John Link to this post

    I know it's only been a day, but time is not a luxury we have right now.  I came up with the following solution.  I don't know if it is the best, but it does not impact the existing styling and plays well with annotations.

    void GridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
     {
         var attributes =
             ((System.ComponentModel.MemberDescriptor) (e.ItemPropertyInfo.Descriptor)).Attributes;
         var displayAttribute = attributes.OfType<System.ComponentModel.DataAnnotations.DisplayAttribute>().FirstOrDefault();
     
         if (displayAttribute != null)
         {
             e.Column.Header = new TextBlock() { ToolTip = "Bob", Text = displayAttribute.GetName() };
         }
     }

  3. John
    John avatar
    6 posts
    Member since:
    Mar 2009

    Posted 15 Jul 2014 in reply to John Link to this post

    Apparently there is no edit.
    If others are looking for this, here is the correct code.


    void GridView_AutoGeneratingColumn(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        var attributes =
            ((System.ComponentModel.MemberDescriptor) (e.ItemPropertyInfo.Descriptor)).Attributes;
        var displayAttribute = attributes.OfType<System.ComponentModel.DataAnnotations.DisplayAttribute>().FirstOrDefault();
     
        if (displayAttribute != null)
        {
            e.Column.Header = new TextBlock() { ToolTip = displayAttribute.GetDescription(), Text = displayAttribute.GetName() };
        }
    }

    Hope this helps someone...


Back to Top