CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

CSLA/WPF - How to bind ToolTip to a DisplayAttribute

rated by 0 users
Answered (Verified) This post has 1 verified answer | 4 Replies | 1 Follower

Top 500 Contributor
19 Posts
cconte posted on Thu, Apr 12 2012 9:05 AM
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 csla businessBase class, I have this property :

public static PropertyInfo<string> DescriptionProperty = RegisterProperty<string>(c => c.Description);
        [Display(Name= "Description")]
        [StringLength(140)]
        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,
Cedric

Answered (Verified) Verified Answer

Top 10 Contributor
1,772 Posts
Answered (Verified) JonnyBee replied on Thu, Apr 12 2012 10:05 AM
Verified by cconte

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.

Jonny Bekkum, Norway CslaContrib Coordinator

All Replies

Top 10 Contributor
1,772 Posts
Answered (Verified) JonnyBee replied on Thu, Apr 12 2012 10:05 AM
Verified by cconte

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.

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
19 Posts
cconte replied on Thu, Apr 12 2012 11:16 AM

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.

Cedric

Top 10 Contributor
1,772 Posts

In .NET 4.5 you can bind to static property (new for .Net 4.5) .
http://msdn.microsoft.com/en-us/library/bb613588%28v=vs.110%29.aspx#static_properties

Workaround for previous versions is to create a dummy instance of the class in resource and use as source:
http://stackoverflow.com/questions/936304/binding-to-static-property

Jonny Bekkum, Norway CslaContrib Coordinator

Top 500 Contributor
19 Posts
cconte replied on Fri, Apr 13 2012 3:31 AM

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 :


xmlns:lib="clr-namespace:Library;assembly=Library"

        <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.

Page 1 of 1 (5 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems