Custom ItemLabelFormat

2 posts, 0 answers
  1. rachel
    rachel avatar
    36 posts
    Member since:
    Sep 2010

    Posted 06 Dec 2011 Link to this post

    i'm using with the pie.
    my ItemLabelFormat #%{P2} #XCAT becouse i want to display the category name.
    my category name is enum, and i want to display the description by the enum.
    Hou could i do that?
  2. Petar Marchev
    Admin
    Petar Marchev avatar
    968 posts

    Posted 08 Dec 2011 Link to this post

    Hi Rachel,

    There is no way to achieve this out of the box.
    One way to accomplish your goal is to create an additional property in your business class which returns this metadata.

    public class Product
    {
     public ProductName Name { get; set; }
     public double Value { get; set; }
     
     private string nameDescription;
     public string NameDescription
     {
      get
      {
        if(this.nameDescription == null) this.nameDescription = GetEnumDescription(this.Name);
        return this.nameDescription;
      }
    }
     
    public static string GetEnumDescription(Enum value)
    {
      FieldInfo fi = value.GetType().GetField(value.ToString());
     
      DescriptionAttribute[] attributes =
        (DescriptionAttribute[])fi.GetCustomAttributes(
        typeof(DescriptionAttribute),
        false);
     
      if (attributes != null &&
        attributes.Length > 0)
        return attributes[0].Description;
      else
        return value.ToString();
     
     }
    }
     
    public enum ProductName
    {
      [Description("Xbox descr")]
      Xbox,
     
      [Description("TvSet descr")]
      TvSet,
     
      [Description("Telephone descr")]
      Telephone,
     
      [Description("Desktop descr")]
      Desktop
    }

    Now, you need to set the FieldName in the ItemMapping to be this new property.
    <telerik:ItemMapping DataPointMember="XCategory" FieldName="NameDescription" />

    Hope this helps.

    Best wishes,
    Petar Marchev
    the Telerik team

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

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top