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 368881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:51:59+00:00 2026-05-12T13:51:59+00:00

So I am working through my first WPF project and I am liking what

  • 0

So I am working through my first WPF project and I am liking what I see so far. There was more of learning curve than what I anticipated, but nevertheless WPF is pretty cool. However, I am struggling a little bit with the data binding concepts. One specific question I have is how do I make my data binding declarations refactor safe? Consider this example.

public class MyDataObject
{
  public string FooProperty { get; set; }
}

void Bind() 
{
  var gridView = myListView.View as GridView;
  gridView.Columns.Clear();
  gridView.Columns.Add(
    new GridViewColumn() 
      { 
        Header = "FooHeader", 
        DisplayMember = new Binding("FooProperty")
      }
    );
  List<MyDataObject> source = GetData();
  myListView.ItemsSource = source;
}

So what if I rename the FooProperty on my data object to something else? The data binding will be invalid and I will not get a compile error since the binding was declared via text only. Is there a way to make the binding a little more refactor safe?

  • 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-05-12T13:51:59+00:00Added an answer on May 12, 2026 at 1:51 pm

    You could use a lambda expression to express the property name, rather than using the name directly :

        protected static string GetPropertyName<TSource, TResult>(Expression<Func<TSource, TResult>> expression)
        {
            if (expression.NodeType == ExpressionType.Lambda && expression.Body.NodeType == ExpressionType.MemberAccess)
            {
                PropertyInfo prop = (expression.Body as MemberExpression).Member as PropertyInfo;
                if (prop != null)
                {
                    return prop.Name;
                }
            }
            throw new ArgumentException("expression", "Not a property expression");
        }
    

    You would use it like that :

    ...
    DisplayMember = new Binding(GetPropertyName((MyDataObject o) => o.FooProperty))
    ...
    

    OK, it’s a bit verbose… If you want something shorter, you could also create a helper method :

    public Binding CreateBinding<TSource, TResult>(Expression<Func<TSource, TResult>> expression)
    {
        return new Binding(GetPropertyName(expression))
    }
    
    ...
    DisplayMember = CreateBinding((MyDataObject o) => o.FooProperty)
    ...
    

    That way, the refactoring should work fine if you rename the property (except in the XAML of course…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working through the book Learning WCF and on the first tutorial lab
I have been working through a very small-scale WPF project in order to familiarize
I'm working through the Stanford iPhone podcasts and have some basic questions. The first:
I'm working on a simple WPF example and wanted to expand the example but
I'm working on my first WPF application and I'm testing out a custom control
I'm currently working on my first WPF application, and I'm curious as to whether
I'm working through this tutorial: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-3.php At first he has you create a setter
Groovy noob here, I'm working through my first Groovy book and it has example
I'm working my way through 'head first design patterns' and want to use this
I am working through my first VBA book and would appreciate if someone would

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.