如何访问位于ControlTemplate中的WPF控件? [英] How to access a WPF control located in a ControlTemplate?
本文介绍了如何访问位于ControlTemplate中的WPF控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通常,WPF控件在.xaml文件中声明,而不是在后面的代码(.xaml.cs文件)中声明.但是,有时我需要在后面的代码中使用其中一些控件来进行操作.如果该控件驻留"在xaml文件中,该如何获取它的句柄?
Usually, the WPF controls are declared in the .xaml files and not in the code behind (.xaml.cs files). However, sometimes I need to use some of those controls in code behind in order to manipulate them. How can I get the handle of such a control if it "resides" in the xaml file?
推荐答案
您可以使用ControlTemplate类的FindName()方法.
You can use the FindName() method of the ControlTemplate class.
// Finding the grid that is generated by the ControlTemplate of the Button
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
这篇关于如何访问位于ControlTemplate中的WPF控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文