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 ?
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); }
}
<TextBox Text="{Binding Model.Description}" ToolTip="{Binding XXX}"/>
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.
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
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
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.
Copyright (c) Marimer LLC