Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hunt down WPF binding errors?

Tags:

c#

binding

wpf

We have a very large project. The Visual Studio debug output log contains several repeating WPF binding errors. For example:

System.Windows.Data Error: 5 : Value produced by BindingExpression is not valid for target property.; Value='NaN' BindingExpression:Path=Width; DataItem='ContentPresenter' (Name=''); target element is 'ContentPresenter' (Name=''); target property is 'MaxWidth' (type 'Double')

The lines are printed when some action is performed. However, this is a very heavy operation, in which tens of WPF classes are involved.

Is there a quick way to find the exact source of the binding error? Some tool which might help?

like image 627
Elad Avatar asked Aug 03 '10 14:08

Elad


People also ask

How do I fix XAML binding failures?

To enable this experience, go to “Options > Environment > Preview Features”, “XAML Binding Failure Window” and restart Visual Studio.

How do I debug XAML?

Navigate to Tools –> Options –> Debugging –> General –> Enable UI Debugging Tools for XAML . In case you are not able to inspect you xaml and not able to see it in Live property explorer you need to check it. With the option selected, the both xaml inspection and live visual tree work seamlessly for any xaml debugging.

What is ItemsSource binding WPF?

ItemsSource can be data bound to any sequence that implements the IEnumerable interface, although the type of collection used does determine the way in which the control is updated when items are added to or removed.


1 Answers

The error you're seeing is because the MaxWidth of a control is being bound to the Width of another control. MaxWidth has to have a definite numeric value, but Width can have several non-definite values, depending on the layout being used. In this case, the width of the source control is NaN - which is an invalid value for MaxWidth. That's causing the error.

So, I'd be looking for a binding on a control where you're setting MaxWidth="{Binding Width, ElementName=someElement}", or similar.

At a guess, that binding has been put in place because a control is contained within a layout panel like a StackPanel that doesn't constrain the size of its children, and someone's tried to bind MaxWidth to deal with clipping issues. A better solution is to change to a panel control that constrains its content size.

The operation that's being performed is likely nothing to do with the error in this case, except that it seems to be invalidating your layout.

like image 63
Dan Puzey Avatar answered Sep 30 '22 19:09

Dan Puzey