Geeks With Blogs


What I do:

Identity Mine

MVVM Light


What I am:

Microsoft Most Valuable Professional, Client Application Development

Microsoft Certified Technology Specialist, Windows Presentation Foundation

WPF disciples


View my profile on LinkedIn

Creative Commons License
Diary of a Code Trotter by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License

All source code on this blog is licensed under the MIT license.

Copyright (c) 2006 - 2011 GalaSoft Laurent Bugnion

Laurent Bugnion (GalaSoft) Diary of a Code Trotter
Debugging the XamlParseException (continued)
In a previous post, I mentioned how you can get more details about the XamlParseException thrown when something is wrong with the XAML (but you don't know what exactly). Well, a reader named Daniel Puzey made a comment to that, and I think it's worth posting that again:
There's another trick to this. Often, by default, you'll get an error reported at Line 1 of the xaml, which is an obvious lie. You can catch the original exception, though:
- Open the "Exceptions" window (Debug/Exceptions) in Visual Studio.
- Click "add"
- Add "System.Windows.Markup.XamlParseException"
- Check the box to break on throw for this exception.
- Hit F5!
You'll find that the XamlParseException you catch is much more descriptive, and will give the correct position in the xaml file.
How to find binding errors?
Another annoying thing when the XAML is wrong is that binding errors get swallowed silently. You don't know what's wrong, but the binding doesn't work as expected. There is a little-known way to find out more about what went wrong. For example, the following code doesn't work, but no error is shown (well, in that case it's easy to find out: The ElementName refers to mainWindow2, which doesn't exist (should be mainWindow).
<Window x:Class="WindowsApplication2.Window1"

Title="WindowsApplication2" Height="300" Width="300"
<TextBlock Text="{Binding ElementName=mainWindow2, Path=Title}" />

To find out what's wrong, do the following:
  • Execute your application in debug mode (by pressing F5) in Visual Studio
  • Check the window "Output" (if missing, this window is displayed by choosing the menu "Debug / Windows / Output"
  • Make sure that the "Show output from" combo is set to "Debug".
  • The binding error can be found, usually on the last line:
Binding error in output window (debug mode)
Binding error in output window (debug mode)
In that case, the error's full text helps a lot:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=mainWindow2'. BindingExpression:Path=Title; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
Expression Blend e-training
I am in the middle of an e-training for Expression Blend. It's quite OK, the tool is good, the training reviews most important features, and it is fun to learn Blend that way. However the training was really written with designers in mind, like the following snapshot proves:
Expression Blend for designers, training
Yeah, well I may not be cool, but I sure know how to spell "than" ;-)
Posted on Monday, April 2, 2007 10:36 PM Technical stuff , .NET , WPF , Expression Blend | Back to top

Comments on this post: WPF: Debugging the XAML (continued) / Expression Blend training

Comments are closed.
Comments have been closed on this topic.
Copyright © Laurent Bugnion | Powered by: