PropertyInfo.FriendlyName - Intended Use?PropertyInfo.FriendlyName - Intended Use?
Old forum URL: forums.lhotka.net/forums/t/6888.aspx
Tom_W posted on Friday, May 01, 2009
Hi
Beginner's question; what is the FriendlyName property actually for?
When I read about it in the book I was assuming it in some way worked like the <DisplayName()> property declaration attribute in System.ComponentModel and so when I dragged a field onto a winform from a datasource, or bound a grid to a datasource the FriendlyName would be used in place of the actual property name.
Having tried this I realise it isn't the case, so I'm wondering what the correct way to use the FriendlyName is?
Ideally I would like to avoid having to manually change the autogenerated labels and grid column names, and also avoid adding <DisplayName> attributes to every property (which would mean coding a second string literal into the class), is there a way to achieve this? I guess I could just not set the friendly name and instead set the display name attribute, which would give the same number of string literals, but it would be neater to use the friendly name property.
Many Thanks,
Tom
JoeFallon1 replied on Friday, May 01, 2009
I implemented it on my own and then Rocky introduced it to the framework soon after. It is used to display nice verbiage for broken rules.
Rather than say "acctcode is required" it could say "Account code is required."
Once it is defined, *leveraging* it for use in the UI is another question. I have not gone down that road myself. Maybe others have some input.
JoeTom_W replied on Friday, May 01, 2009
[Bangs head against desk]... I'm an idiot, yes of course that's where it's used. Thank you.
I'd be interested to here if anyone has managed to wire it up to a winforms UI though?
RockfordLhotka replied on Friday, May 01, 2009
If you are using 3.6 there are a couple things you might consider.
One is to make your PropertyInfo<T> fields public instead of private. That more closely follows the dependency property model of WPF/WF, and the result is that the UI can leverage the metadata in those fields (including the friendly name).
Another is to expose the FieldManager.GetRegisteredProperties() data to the UI though a public method. CSLA doesn't make this public by default to avoid cluttering the public interface, but there's no reason you couldn't create a public method that delegated to this non-public method.
Again, the result is that you'd allow the UI to use the metadata in the PropertyInfo<T> objects.
JonnyBee replied on Monday, May 04, 2009
Hi,
And for applications running in more than one locale (language) you should put FriendlyName into a resource file so you can translate the friendly name for each locale.
/jonnybee
Tom_W replied on Tuesday, May 05, 2009
@Rocky: Thanks Rocky, that's useful info and yep I'm on 3.6.2. Is there anyway you know of to automate getting the FriendlyNames into say grid column headers, or is it a case of manually coding it all?
@JonnyBee, thank you, that's good advice.
RockfordLhotka replied on Tuesday, May 05, 2009
I don’t know of an automated way to get the friendly names
into the UI, no.
Rocky
Copyright (c) Marimer LLC