先看一段XAML代码:
?
<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对象,可以这样去做:
?
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^)/...