Reputation: 521
How to bind the attributes of model properties to a view?
I have a model, for example:
class MyModel
{
[Display(
ResourceType = typeof(Resources.Strings),
Name = "MyName",
Description = "MyDescription")]
public double MyProperty { get; set; }
}
And my view model is something like this (none unusual):
public class MyVM
{
private Models.MyModel model;
public string MyVMProperty
{
get { return model.MyProperty.ToString(); }
set
{
// some usual code with parsing a value from textbox...
}
}
}
And in a view I want to bind the data from the attributes of model properties. Something like this:
<Grid Name="myGrid">
<TextBox
Text="{Binding Path=MyVMProperty}"
/>
<TextBlock
Text="{Binding Path=MyVM.model.MyProperty.DisplayAttribute.Name}"
/>
<TextBlock
Text="{Binding Path=MyVM.model.MyProperty.DisplayAttribute.Description}"
/>
</Grid>
And somewhere in code I do:
myGrid.DataContext = new MyVM();
How to get it?
Upvotes: 0
Views: 1344
Reputation: 521
One of the possible solution is to add helper class and pass the instance of that class as converter parameter. The instance must be initialized in XAML by hands. The instance consist of all the data required to obtain the value of the attribute of the property of the model instance. The solution is generalized and there are no hardcoded data inside of the converter logic. The "value" parameter of the converter is not required also.
So, the result looks like this:
Something in the same assembly as view model. Converter and helper class (simplified - without any null checks etc.):
namespace MyProject.Converters
{
public class MetadataParameters
{
public Type ModelType { get; set; }
public string ModelProperty { get; set; }
public Type AttributeType { get; set; }
public string AttributeProperty { get; set; }
}
public class MetadataConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var mp = parameter as MetadataParameters;
var modelPropertyInfo = mp.ModelType.GetProperty(mp.ModelProperty);
var attribute = modelPropertyInfo
.GetCustomAttributes(true)
.Cast<Attribute>()
.FirstOrDefault(memberInfo => memberInfo.GetType() == mp.AttributeType);
var attributeProperty = attribute.GetType().GetProperty(mp.AttributeProperty);
return attributeProperty.GetValue(attribute, null);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
In resource file (XAML):
xmlns:converters="clr-namespace:MyProject.Converters"
...
<converters:MetadataConverter x:Key="metadataConverter" />
In view file:
<!-- language: lang-xml -->
xmlns:converters="clr-namespace:MyProject.Converters"
xmlns:DataAnnotations="clr-namespace:System.ComponentModel.DataAnnotations;assembly=System.ComponentModel.DataAnnotations"
xmlns:Models="clr-namespace:MyProject.Models"
...
<TextBlock
<TextBlock.Text>
<Binding
Mode="OneWay"
Converter="{StaticResource metadataConverter}">
<Binding.ConverterParameter>
<converters:MetadataParameters
ModelType="{x:Type Models:Model}"
ModelProperty="ModelProperty"
AttributeType="{x:Type DataAnnotations:DisplayAttribute}"
AttributeProperty="Name" />
</Binding.ConverterParameter>
</Binding>
</TextBlock.Text>
</TextBlock>
Upvotes: 1
Reputation: 437386
Since attribute properties can only be retrieved using reflection, you will have to provide some means of converting a "(class OR object) + property name" tuple to something that exposes the values of these attributes.
A very specific solution would look like
public DisplayAttribute MyVMPropertyDisplayAttribute
{
get
{
var prop = typeof(MyModels.Model).GetProperty("MyProperty");
var attr = prop.GetCustomAttributes(typeof(DisplayAttribute));
return attr.Cast<DisplayAttribute>().Single();
}
}
and you could then bind to e.g. MyVMPropertyDisplayAttribute.Name
.
Obviously this solution needs to be generalized so that it can be used conveniently across different model and attribute types; it would be a good idea to package it inside a value converter instead.
Upvotes: 1