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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:50:49+00:00 2026-06-04T16:50:49+00:00

To make sure that a binding in WPF is targeting an existing property, I’m

  • 0

To make sure that a binding in WPF is targeting an existing property, I’m using static property-name-properties.

Now I wan’t to encapsulate more info about my properties into a static property description object, Name, Type, Id etc, but without having to have one path-bindable property for the name and one property with all other info.

The problem is that WPF complains about the property having the wrong type, not String but PropertyInfo.

I’m trying to get around this limitation somehow. For instance I have tried making my PropertyInfo implicitly castable to string, overriding ToString and adding both a TypeConverter from PropertyInfo to string and to string from PropertyInfo. Nothing works.

And I can’t bind directly to the Name-property either.

<TextBlock Text="{Binding Path={x:Static l:Test.TitleProperty}}" />

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        TypeDescriptor.AddAttributes(typeof(string),
          new TypeConverterAttribute(typeof(StringFromPropertyConverter)));

        DataContext = new Test { Title = "hello" };
    }
}

public class Test
{
    public static readonly PropertyInfo TitleProperty = 
      new PropertyInfo { Name = "Title" };

    public string Title { get; set; }
}

[TypeConverter(typeof(PropertyToStringConverter))]
public class PropertyInfo
{
    public string Name { get; set; }

    public static implicit operator string(PropertyInfo p) { return p.Name; }

    public override string ToString()
    {
        return Name;
    }
}

public class PropertyToStringConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context,
      Type destinationType)
    {
        if (destinationType == typeof(string)) return true;
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, 
      System.Globalization.CultureInfo culture, object value,
      Type destinationType)
    {
        return ((PropertyInfo)value).Name;
    }
}

public class StringFromPropertyConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context,
      Type sourceType)
    {
        if (sourceType == typeof(PropertyInfo)) return true;
        return base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, 
      System.Globalization.CultureInfo culture, object value)
    {
        return ((PropertyInfo)value).Name;
    }
}

Any suggestions?

  • 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-04T16:50:50+00:00Added an answer on June 4, 2026 at 4:50 pm

    Your PropertyInfo needs to have a TypeConverter to convert PropertyInfo to System.Windows.PropertyPath, not string. Also, you might want to think about reusing .NETs System.Reflection.PropertyInfo for this purpose.

    I have seen this approach before and the main reason for it was to avoid “magical strings” in property change notifications. So you might also take a look how to accomplish to get a PropertyInfo using System.Linq.Expressions like this:

    public static class ReflectionHelper
    {
        public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> getter)
        {
            return (PropertyInfo)((MemberExpression)getter.Body).Member;
        }
    }
    

    And use it like this:

    public class Test
    {
        public static readonly PropertyInfo TitleProperty = ReflectionHelper.GetPropertyInfo<Test>(x => x.Title);
    
        public string Title { get; set; }
    }
    

    EDIT : NEW ANSWER

    Yes, you are right. It doesn’t work even if you define TypeConverter to convert PropertyInfo to System.Windows.PropertyPath. I think it is because the ConverterType attribute should be placed on System.Windows.PropertyPath class. But since it is a class that you don’t own, you can’t place attributes on it. Using the TypeDescriptor to add attribute won’t work because XAML doesn’t use TypeDescriptor infrastructure.

    You can accomplish your conversion with MarkupExtension. Here is a complete code (it uses PropertyInfo from System.Reflection namespace):

    ReflectionHelper.cs

    using System;
    using System.Linq.Expressions;
    using System.Reflection;
    
    namespace WpfApplication
    {
        public static class ReflectionHelper
        {
            public static PropertyInfo GetPropertyInfo<T>(Expression<Func<T, object>> getter)
            {
                return (PropertyInfo)((MemberExpression)getter.Body).Member;
            }
        }
    }
    

    Test.cs

    using System.Reflection;
    
    namespace WpfApplication
    {
        public class Test
        {
            public static readonly PropertyInfo TitleProperty = ReflectionHelper.GetPropertyInfo<Test>(x => x.Title);
    
            public string Title { get; set; }
        }
    }
    

    PropertyInfoPathExtension.cs

    using System;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Markup;
    
    namespace WpfApplication
    {
        public class PropertyInfoPathExtension : MarkupExtension
        {
            private readonly PropertyInfo propertyInfo;
    
            public PropertyInfoPathExtension(PropertyInfo propertyInfo)
            {
                if (propertyInfo == null)
                    throw new ArgumentNullException("propertyInfo");
    
                this.propertyInfo = propertyInfo;
            }
    
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                return new PropertyPath(propertyInfo);
            }
        }
    }
    

    MainWindow.xaml

    <Window x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication"
            Title="MainWindow" Height="350" Width="525">
        <Window.DataContext>
            <local:Test Title="hello"/>
        </Window.DataContext>
        <TextBlock Text="{Binding Path={local:PropertyInfoPath {x:Static local:Test.TitleProperty}}}"/>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i make sure that several divs are slided up? Right now I
I want to make sure that if any error occurs during the database processing
I would like to make sure that an applescript can be converted to bash.
I'm trying to make sure that my Managed to Unmanaged calls are optimized. Is
I wish to make sure that my data has a constraint the following check
how do I make sure that UIInterfaceOrientationPortraitRight and UIInterfaceOrientationPortraitLeft are not supported. Basically I
I want to make sure that all 3 conditions result in the same answer
I need to make sure that the height of the header of table is
I want to make sure that no value is cut off in a workbook
I want to make sure that I only print maximum 80 character long lines,

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.