Monday, September 25, 2017

Tracking down errors thrown by the framework

I had a weird problem today. I have a page that allows the users to view change orders. A certain type of change orders displayed errors when an expander was expanded. There is a DataGridComboBoxColumn in the expander that is only displayed for this type of change order. The error message gave no indication of what code was throwing the error. The error message said...

"TextBlock" TargetType does not match type of element "TextBlockComboBox"

I should have picked up on the word "TargetType" but I didn't.

I started by assuming it was a converter issue so I set breakpoints on the Convert method of each of my converters. This is easy to do in Visual Studio 2015.

I hit Ctrl-B to open the new Function Breakpoint dialog box and then entered "Convert" in the Function Name.


Clicking [OK] sets a break point on every method named "Convert" although it does not update the UI so you just have to trust it. I then disabled all breakpoints, moved through the program to the point of failure, enabled all breakpoints and caused the program to fail. None of the breakpoints were hit. OK, so it's not a converter problem.

Then I thought it might be a CanExecute problem. I've noticed that command CanExecute methods are often not called until the commands' controls are visible. I put breakpoints on all those methods and none of them were hit either.

In my past experience any style problems are raised by InitializeComponent but eventually I decided to double-check the styles on the DataGridComboBoxColumn that I suspected was causing the problem. Sure enough I found ElementStyle="{StaticResource EnabledTextBlock}" and it turned out the style was defined as <Style TargetType="{x:Type TextBlock}" x:Key="EnabledTextBlock">. One of my minions had changed the column definition last week and it had not been properly tested.

So the short story is that when the framework raises error messages complaining about TargetType, it's a style problem.

No comments:

Post a Comment