object obItem=this.FindResource("canvasdt");
if (obItem is System.Windows.DataTemplate)
{
System.Windows.DataTemplate getDtemplate = obItem as System.Windows.DataTemplate;
if (getDtemplate.LoadContent() is Border)
{
Border b = getDtemplate.LoadContent() as Border;
if (b.Child is Grid)
{
if ((b.Child as Grid).FindName("toolStackPanel") is StackPanel)
{
//((b.Child as Grid).FindName("toolStackPanel") as StackPanel).DataContext = CKInstanceBase<CommandBind>.Instance;
}
}
}
}
上面仅仅举例,如果多级,这递归即可