Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

Okay, so I've not managed to find anything to help me with this on Google (although I'm not sure I have been searching for the right thing, if not I apologize).

My question is,

If I have a view model like such:

C#
internal class NewDataEntriesViewModel
{
    private CRCSubmissionDataEntry m_Entries;
   
    public NewDataEntriesViewModel(CRCSubmissionDataEntry; dataEntries)
    {
            m_Entries = dataEntries;
    }

    #region Properties
    [Description("Entry Name")]
    Public string EntryName
    {
       get {return m_EntryName;}
    }
    #endregion

}


In my View for this ViewModel, how can I bind a label to the Description attribute on my property?
Is this even possible?

Thanks.
Posted
Comments
Sushil Mate 12-Jul-13 6:34am    
why would you do that? whats the use of
Pheonyx 12-Jul-13 7:03am    
I have a large viewmodel, about 20 properties,
on the view each one needs a label to go with its data entry. I have a choice, I can either create another 20 properties just for the label text, or I can use attributes on my viewmodel, and bind to the same property as its data entry control to display its name.

I don't really want to have 40 properties on a viewmodel, 20 is to many in my opinion but I've not found a way to reduce that yet.
Pheonyx 12-Jul-13 7:03am    
It also means you can clearly see the associated friendly name of a property in your code.

Yes, it is possible. You can do it using Reflection. Check this

http://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx[^]
 
Share this answer
 
I finally found a solution!

The following thread gives details on how to create a Converter to use with the databinding. It could easily be enhanced to to different types of attributes, however it is hard coded for the description attribute which is perfect for my requirement.

It was partial down to Syed's comment about reflection that helped me find it.

Link:

Binding to a Propertys Attribute[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900