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

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

2 Answers 261 Views
GridView
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 14 Jul 2014, 07:43 PM
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 Answers, 1 is accepted

Sort by
0
John
Top achievements
Rank 1
answered on 15 Jul 2014, 07:41 PM
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() };
     }
 }

0
John
Top achievements
Rank 1
answered on 15 Jul 2014, 07:53 PM
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...


Tags
GridView
Asked by
John
Top achievements
Rank 1
Answers by
John
Top achievements
Rank 1
Share this question
or