Not quite sure that I understand the script, but since you mention a DataTemplate, I assume you are using a template.
If you use a template, then what you do gives your border a name (x: Name = "border"), and then overrides the OnApplyTemplate method. In this method, you use GetTemplateChild and pass in the name that you used. This will return a link to your border.
If you are not using a template and have a link to ContentPresenter, then you can write a recursive function that considers the Content property for the child element and if it is not a border, then the same function is called in its contents.
source share