The template of a control is only applied whether the control is in the visual tree and is visible.
So the behavior you mentionned is normal.
The workaround might be to set the legend as visible by default and then set it to 'Collapsed' on event 'Loaded'.