0

I have this C# object ( Current, Monitor.CarNumber value is 123456 )

    public class Monitor
    {
        [Display(Name = "This is car number")]
        public string CardNumber { get; set; }
    }

And in WPF, An Label control display Monitor.CardNumber

<Label Content={Binding CardNumber} /> 

( When I run the program. it display 123567, It work correctly ).

Now I don't want it display the CardNumber's value, I want it display the "This is car number" ( the Data Annotation of CardNumber Property ).

How should I do that ? ( No C# code, only modify above XML code )

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • WPF has no built-in support for the `Display` attribute, and there is no XAML syntax for reading attributes. So unfortunately your requirement of "no C# code" can't be met. – nmclean Jan 15 '14 at 16:48

1 Answers1

1

You should use a combination of Converter and Reflection to get the Display Name in binding.

When you bind your label with CardNumber, the Converter will look for the attached attribute using Reflection and if it find DisplayAttribute, will return the Name property from attribute for Binding, else return the CardNumber.

This answer on SO will help you to get the Display.Name using Reflection.

This doesn't looks as simple as I have answered, since you have to Pass the Property Name to the converter.

Community
  • 1
  • 1
Kishore Kumar
  • 12,675
  • 27
  • 97
  • 154