Template in tree item

I have a tree view that looks like


This works just fine. Now I would like to further format the expanded item like

But this gives me an invalid cast exception.

What am I doing wrong?