Skip to content

How to bind ToolTip to a DisplayAttribute

cedric conte

Asked Apr 12, 2012, 6:11 AM EDT1 answer
Share
LinkedInX
Hi,
In my BO, I have added DisplayAttributes to my properties, and would now like to bind the ToolTip property of my TextBox controls to the Description property of the DisplayAttribute.
Is  WPF and silverlight support the DisplayAttribute dataAnnotation ?

Here what I'm trying to do:
In my basic class, I have this property :

[Display(Name = "Description")]
 public string Description { get; set; }

Below the XAML code. Actually, I don't know how to realize the binding with the ToolTip property of my TextBox controls. That's why I have write XXX.

<TextBox Text="{Binding Description}" ToolTip="{Binding XXX}"/>

Thx in advance for your help,
Cedric

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Senthilkumar
    0
    Hi,

    For example,


    <Grid >
        <StackPanel>
            <TextBox Text="{Binding FirstName}" ToolTip="{Binding Descriptions[FirstName]}"/>
            <TextBox Text="{Binding LastName}" ToolTip="{Binding Descriptions[LastName]}"/>
        </StackPanel>
    </Grid>


    he code-behind:

    C#
    DataContext = new DisplayViewModel();
    
    
    public class DisplayViewModel
    {
        private static Dictionary<string, string> descriptions;
    
    
        static DisplayViewModel()
        {
            descriptions = new Dictionary<string,string>();
            foreach (var propertyName in PropertyNames)
            {
                var property = typeof(DisplayViewModel).GetProperty(propertyName);
                var displayAttributes = property.GetCustomAttributes(typeof(DisplayAttribute), true);
                var displayAttribute = displayAttributes.First() as DisplayAttribute;
                var description = displayAttribute.Name;
                descriptions.Add(propertyName, description);
            }
        }
    
    
        public DisplayViewModel()
        {
            FirstName = "Bill";
            LastName = "Smith";
        }
    
    
        public static IEnumerable<string> PropertyNames { get { return new[] { "FirstName", "LastName" }; } }
    
    
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Display(Name = "Last Name")]
    C#
        public string LastName { get; set; }
    
    
        public IDictionary<string, string> Descriptions { get { return descriptions; } }
    }
    0