2009-10-02 69 views
0

基本上我有一個包含Border,StackPanel和東西的DataTemplate,後來我在許多ContentControl中使用這個DataTemplate作爲ContentTemplate。使用WPF,如何檢索DataTemplate包含的控件?

這些ContentControl被命名,但是從C#代碼中我找不到一種方法來找回我的Border,StackPanel和它們的東西。

任何想法?

回答

1

你應該能夠做到的財產以後這樣的:

// Finding textBlock from the DataTemplate that is set on that ContentPresenter 
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate; 
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("textBlock", myContentPresenter); 

,你可以找到更多關於它在這裏:How to: Find DataTemplate-Generated Elements

+0

這幫助了我,但我必須將ContentControl更改爲ContentPresenter,否則會引發錯誤。這裏有關於ContentControl的問題的詳細解釋:http://joshsmithonwpf.wordpress.com/2007/06/28/how-to-use-findname-with-a-contentcontrol/ – TigrouMeow 2009-10-02 12:03:14

相關問題