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

  • Home
  • SEARCH
  • 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 3215406
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:10:26+00:00 2026-05-17T15:10:26+00:00

In MVVM it is normal to connect View to the ViewModel with data binding.

  • 0

In MVVM it is normal to connect View to the ViewModel with data binding.

Therefore if the name of a properties changes on one of the Model objects that is databound to there is no compiler error.

When the compiler will not stop a bug, the next thing I think of is “UnitTest”, However

How do you unit test this without
spending forever writing a GUI test?

Is there a system that will check that all the properties that are bound to is valid, (without having to run the UI) that I can call in a unit test?

I am looking for something that will take the view, and then loop over all WPF controls, for each WPF control it will look at all the binding and check if they are valid.


By the way there have been a few good questions about how to make OnPropertyChanged safe, and/or how to test it (But done of these get down to the level of a WPF view.)

  • How to make Databinding type safe and support refactoring
  • Automatically INotifyPropertyChanged
  • workarounds for nameof() operator in C#: typesafe databinding
  • A Fluent Interface For Testing INotifyPropertyChanged
  • Automatic Class Tester will test all simple proptites and INotifyPropertyChanged

I have put a bounty on this question, as someone must have thought hard about the problem and come up with soltions.

  • 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-17T15:10:27+00:00Added an answer on May 17, 2026 at 3:10 pm

    I think I’ve come up with something that may work using simple reflection, and adapting some code I’ve used in the past (the code for the FindBindingsRecursively method was written by Martin Bennedik’s as part of his Enterprise WPF Validation Control):

    [TestMethod]
    public void CheckWpfBindingsAreValid()
    {
        // instansiate the xaml view and set DataContext
        var yourView = new YourView(); 
        yourView.DataContext = YourViewModel;
    
        FindBindingsRecursively(yourView,
                delegate(FrameworkElement element, Binding binding, DependencyProperty dp)
                {
                    var type = yourView.DataContext.GetType();
    
                    // check that each part of binding valid via reflection
                    foreach (string prop in binding.Path.Path.Split('.'))
                    {
                        PropertyInfo info = type.GetProperty(prop);
                        Assert.IsNotNull(info);
                        type = info.PropertyType;
                    }
        });
    }
    
    private delegate void FoundBindingCallbackDelegate(FrameworkElement element, Binding binding, DependencyProperty dp);
    
    private void FindBindingsRecursively(DependencyObject element, FoundBindingCallbackDelegate callbackDelegate)
    {
        // See if we should display the errors on this element
        MemberInfo[] members = element.GetType().GetMembers(BindingFlags.Static |
                    BindingFlags.Public |
                    BindingFlags.FlattenHierarchy);
    
        foreach (MemberInfo member in members)
        {
            DependencyProperty dp = null;
    
            // Check to see if the field or property we were given is a dependency property
            if (member.MemberType == MemberTypes.Field)
            {
                FieldInfo field = (FieldInfo)member;
                if (typeof(DependencyProperty).IsAssignableFrom(field.FieldType))
                {
                    dp = (DependencyProperty)field.GetValue(element);
                }
            }
            else if (member.MemberType == MemberTypes.Property)
            {
                PropertyInfo prop = (PropertyInfo)member;
                if (typeof(DependencyProperty).IsAssignableFrom(prop.PropertyType))
                {
                    dp = (DependencyProperty)prop.GetValue(element, null);
                }
            }
    
            if (dp != null)
            {
                // Awesome, we have a dependency property. does it have a binding? If yes, is it bound to the property we're interested in?
                Binding bb = BindingOperations.GetBinding(element, dp);
                if (bb != null)
                {
                    // This element has a DependencyProperty that we know of that is bound to the property we're interested in. 
                    // Now we just tell the callback and the caller will handle it.
                    if (element is FrameworkElement)
                    {
                        callbackDelegate((FrameworkElement)element, bb, dp);
                    }
                }
            }
        }
    
        // Now, recurse through any child elements
        if (element is FrameworkElement || element is FrameworkContentElement)
        {
            foreach (object childElement in LogicalTreeHelper.GetChildren(element))
            {
                if (childElement is DependencyObject)
                {
                    FindBindingsRecursively((DependencyObject)childElement, callbackDelegate);
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a WPF application using MVVM (Model-View-ViewModel) pattern and I'd like
I am using the Model-View-ViewModel architecture in a WPF application I am building, and
I use MVVM architecture to decouple my application. That is, you often see something
I'm working on a Silverlight app using the MVVM pattern. My ViewModel currently consists
I'm writing a Silverlight app using the MVVM pattern. I have a main view
What is the difference between normal reflection and the reflection that can be done
I have an MVVM Silverlight 4 application that holds a list of modules (a
In a WPF app that I'm writing using the MVVM pattern, I have a
Considering you have an MVVM Architecture in WPF like Josh Smith's examples How would
Although I deeply fell in love with the MVVM pattern there seem to be

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.