CSLA/WPF - How to bind ToolTip to a DisplayAttribute

CSLA/WPF - How to bind ToolTip to a DisplayAttribute

Old forum URL: forums.lhotka.net/forums/t/11298.aspx

cconte posted on Thursday, April 12, 2012


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 csla businessBase class, I have this property :

public static PropertyInfo<string> DescriptionProperty = RegisterProperty<string>(c => c.Description);
        [Display(Name= "Description")]
        public string Description
            get { return GetProperty(DescriptionProperty); }
            set { SetProperty(DescriptionProperty, value); }

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 Model.Description}" ToolTip="{Binding XXX}"/>

Thx in advance for your help,

JonnyBee replied on Thursday, April 12, 2012

You can bind to Model.DescriptionProperty.FrindlyName or wrap the description in your ViewModel by locating the DisplayAttribute for that property.

When no friendlyname is provided to PropertyInfo it will try to read Display attributt for that property into FriendlyName.

cconte replied on Thursday, April 12, 2012

Thx for your answer JonnyBee,

I'm using the Bxf framework to create the ViewModel. But from the XAML visual studio editor  the DescriptionProperty (PropertyInfo type) seems to be not exposed.

I think it must be possible to get the Display attribut  from the FriendlyName, but so far i wasnt be able to write the right XAML line of code.

May you show me the XAML line of code to make it right.

Thanks a lot for your help.


JonnyBee replied on Thursday, April 12, 2012

In .NET 4.5 you can bind to static property (new for .Net 4.5) .

Workaround for previous versions is to create a dummy instance of the class in resource and use as source:

cconte replied on Friday, April 13, 2012

Nice JonnyBee. that's work.

Below some code and note that's could help people.

BO code :

[Required(ErrorMessageResourceName = "TitleRequired", ErrorMessageResourceType = typeof(Library.Properties.Resources))]
        [Display(Name = "Title", ResourceType = typeof(Library.Properties.Resources))]
        [StringLength(45, ErrorMessageResourceName = "Title45Characters", ErrorMessageResourceType = typeof(Library.Properties.Resources))]
        public string Name
            get { return GetProperty(NameProperty); }
            set { SetProperty(NameProperty, value); }

XAML code :


        <TextBox Grid.Column="1" Grid.Row="2" Name="nameTextBox" ToolTip="{Binding Source={x:Static lib:TypeCdGEdit.NameProperty}, Path=FriendlyName}" Text="{Binding Path=Model.Name, Mode=TwoWay, ValidatesOnExceptions=False, NotifyOnValidationError=False}" Margin="2" />

Note :

Don't forget to change your resource class and properties to public to make it work. Actually, the System.ComponentModel.DataAnnotations attributes is tricky. DisplayAttribute checks the Type.IsVisible  property while ValidationAttribute(RequiredAttribute inherit from them) will not.

Copyright (c) Marimer LLC