views:

62

answers:

2

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?

+3  A: 

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.

Dan Puzey
+1  A: 

Bea Stollnitz has a great overview of debugging WPF data bindings. The "Trace Level" method has worked well for me. If you are using Visual Studio 2010 you will need to fix the trace setting default as described here.

John Myczek