Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9203687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:31:41+00:00 2026-06-17T23:31:41+00:00

How to bind the attributes of model properties to a view? I have a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:31:42+00:00Added an answer on June 17, 2026 at 11:31 pm

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Knockout-JS to bind properties in my view to my view model.
I have a strong typed view model and a MetaData partial class which has
I have a view model class which uses UnitOfWork to some database operations like
What I have is a model which has one of it's attributes dynamic. This
I've been following some tutorials to bind the view with the model in backbone.js,
class TheModel extends Backbone.Model foo: #bar Entity = new TheModel(#pass in attributes) Can I
I am building a web application in Play! 1.2.5. I have a model class
I have a very simple composite model made of two classes: Public Class ParentModelVM
Is it possible to bind a Spring form to two different model attributes? Something
I have noticed that when multiple attributes of a Backbone model are set like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.