WPF中如何获取ControlTemplate中的对象

原文 http://www.silverlightchina.net/html/study/WPF/2010/1116/3418.html

先看一段XAML代码:

 1

 2

 3

 4

 5

 6

 7

 8

 9

10

11

12

13

14

15

16

17

18
<TabItem Name="RootTabItem" Background="Transparent">

	<TabItem.Template>

		<ControlTemplate TargetType="TabItem">

			<Button Name="btnTabItem">

				<Button.Template>

					<ControlTemplate TargetType="Button">

						<StackPanel Orientation="Horizontal" Background="Transparent">

							<local:IconButton x:Name="btnSend" ToolTip="send"

								   Icon="/Resources/Icon/sendIcon.png"/>

						    <local:IconButton x:Name="btnCut" ToolTip="cut"

								   Icon="/Resources/Icon/cutIcon.png"/>

						</StackPanel>

					</ControlTemplate>

				</Button.Template>

			</Button>

		</ControlTemplate>

	</TabItem.Template>

</TabItem>

  我想要获取btnSend对象,可以这样去做:

1

2

3

4
DependencyObject d1 = VisualTreeHelper.GetChild(RootTabItem,0);

Button button = LogicalTreeHelper.FindLogicalNode(d1,"btnTabItem") as Button;

DependencyObject d2 = VisualTreeHelper.GetChild(button,0);

IconButton iconButton = LogicalTreeHelper.FindLogicalNode(d2,"btnSend") as IconButton;

  现在就可以使用iconButton(btnSend)干你想干的事情了,\(^o^)/...

本文来自朔月星辰的博客,原文地址:http://www.cnblogs.com/syxc/archive/2010/11/05/1870147.html

你可能感兴趣的:(template)