display annotation

2 posts, 0 answers
  1. Walter
    Walter avatar
    22 posts
    Member since:
    Jun 2009

    Posted 18 Oct 2011 Link to this post

    Hi,

    I am searching for a workaround to set the label and description property automatically from the display attribute even if the datafrom is not in the mode AutoGenerateFields=True. I started to create a custom control inherited from DataFormDataField and I was able to read out the correct values by reflection within the OnApplyTemplate event handler:

    var dc = this.DataContext;
    if(dc != null)
    {
        Type t =
    dc.GetType();
       
    PropertyInfo pi = 
    t.GetProperty(this.DataMemberBinding.Path.Path);
       
    var attr 
    = pi.GetCustomAttributes(typeof(DisplayAttribute), true).FirstOrDefault();
       
    if(attr != 
    null)
        {
            var
    displayAttr = attr as DisplayAttribute;
           
    ResourceManager 
    rm = new ResourceManager(displayAttr.ResourceType);        
            this.Label = rm.GetString(displayAttr.Name);      
            this.Description = rm.GetString(displayAttr.Description);
        }
    }
    Unfortunately the properties are not displayed initially.
    Is this in general a good approach for the problem? Where is the correct place (event) to execute the code?

    Kind Regards

  2. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1128 posts

    Posted 18 Oct 2011 Link to this post

    Hi Daniel Koffler,

    I have prepared an example project for you that illustrates a possible approach to achieving this task.  Please, refer to it and let us know whether such a solution meets your requirements.

    Regards,
    Ivan Ivanov
    the Telerik team
    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
  3. DevCraft banner
Back to Top