Tuesday, April 26, 2016

Control's IsEnabled is inherited from the parent control

Two posts in two days. Must be having a bad week.

I spent four hours trying to figure this one out. The problem was that the user is unable to select a row in a datagrid and the datagrid looked disabled. The datagrid is inside a user control.

I put a breakpoint in a routed command's can_execute method (very handy for debugging) and quickly determined that the datagrid's IsEnabled property is false. Then it gets weird - I set it to true in the watch window and it immediately gets set false again even though it is a read/write property.

So I added an IsEnabledChanged event handler in the initialization code. When the event handler was called to set the IsEnabled property false I looked at the call stack, but it just says "External Code". So my code isn't the problem.

The user control is inside an expander which is initially collapsed. I notice the datagrid is only being disabled as the expander expands, ie when the user control is initially rendered. I have no Loaded event handler.

I took all the styles off the datagrid in case I had some kind of trigger on a style but that didn't fix the problem. I don't have a default style for datagrids.

Then I went home because it was late and I was making no progress. Plus it was snowing at my house and I had to get home in my Prius.

The next morning I noticed the user control's IsEnabled property is false and I can't change it to true in the watch window. Same behaviour as the datagrid. I wondered if the datagrid's IsEnabled property is inherited from the user control.

Then I started to look for the code that was disabling the user control. Found it! When I modified the code to not disable the user control everything worked correctly.

It's amazing how sleeping on an intransigent problem can help you solve it. It's tempting to keep working on a problem but sometimes walking away from it for a while can be the right thing to do.

No comments:

Post a Comment