WPF 获得Template中的控件

关键代码

var template = checkBox1.Template;
var myControl = template.FindName("textBlock1", checkBox1);
var tb = myControl as TextBlock;
tb.Background = new SolidColorBrush(Colors.LightPink);

示例代码

ControlTemplateDemo.xaml
ControlTemplateDemo.xaml.cs

参考资料

How do I access an element of a control template from within code-behind

posted @ 2020-08-13 19:46  Lulus  阅读(461)  评论(0编辑  收藏  举报