2009-08-18 15 views
1

假設我想將CheckBox放在Button的內部。無論如何,我可以在代碼中引用該複選框嗎?即在Window1.cs中,我想寫下類似於:testButton.innerCheckBox.DoStuff();如何引用控件模板中的元素

<ControlTemplate TargetType="{x:Type Button}">       
     <Microsoft_Windows_Themes:ButtonChrome SnapsToDevicePixels="true" x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Fill="{TemplateBinding Background}" RenderDefaulted="{TemplateBinding IsDefaulted}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" ThemeColor="NormalColor"> 
     <Grid Width="32.083" Height="13.277"> 
      <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Stretch" Margin="{TemplateBinding Padding}" VerticalAlignment="Stretch" RecognizesAccessKey="True" d:LayoutOverrides="Width, Height"/> 
      <CheckBox x:Name="innerCheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" Content="CheckBox"/> 
     </Grid> 
     </Microsoft_Windows_Themes:ButtonChrome> 
    </ControlTemplate> 

回答

4
CheckBox innerCheckBox = testButton.Template.FindName("innerCheckBox", testButton) as CheckBox; 
+0

我喜歡這個網站。感謝您的快速響應。 – Kelly 2009-08-18 16:33:56

相關問題