Reputation: 71
[DisplayName("Planned Amt($):")]
public string PlannedAmount { get; set; }
[DisplayName("Unallocated Amt($):")]
public string UnallocatedAmount { get; set; }
I have this members in my class .Based on budgetType variable value i need to change the DisplayName Value for both the attributes. Please, let me know how to do that.
Upvotes: 7
Views: 2943
Reputation: 855
You can inherit from DisplayNameAttribute like this:
public class BudgetDisplayNameAttribute : DisplayNameAttribute
{
public BudgetDisplayNameAttribute(string pBaseName) : base(pBaseName) { }
public override string DisplayName
{
get
{
return My.BudgetType + " " + base.DisplayName;
}
}
}
Where, for this example, My.BudgetType is a public static string in a public static class:
public static class My
{
public static string BudgetType = "Quarterly";
}
And then apply the attribute to your property like this:
[BudgetDisplayName("Planned Amt($):")]
public string PlannedAmount { get; set; }
[BudgetDisplayName("Unallocated Amt($):")]
public string UnallocatedAmount { get; set; }
Finally, the part of your program that uses the DisplayName attribute of your properties will get the text:
"Quarterly Planned Amt($):" for the PlannedAmount property
"Quarterly Unallocated Amt($):" for the UnallocatedAmount property
Upvotes: 2
Reputation: 1264
You should just be able to inherit from DisplayNameAttribute and override the DisplayName property:
public class DisplayNameExAttribute : DisplayNameAttribute
{
public override string DisplayName
{
get
{
// do what you want here.
return base.DisplayName;
}
}
}
I haven't actually tried to implement this, so it is possible that the DisplayName attribute is coded such that some other extension point will have to be used, but I'll try to confirm.
Upvotes: 3