views:

57

answers:

2

Basically I have a DataTemplate that contains Border, StackPanel and stuff, and later I use this DataTemplate as a ContentTemplate in many ContentControl(s).

These ContentControl(s) are named, but from the C# code I don't manage to find a way to get back my Border, StackPanel and stuff from them.

Any ideas?

A: 

Try FindResource()

here's an example

Natrium
+1  A: 

You should be able to do somthing like this:

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

And you can find more about it here: How to: Find DataTemplate-Generated Elements

HackerBaloo
This helped me, but I had to change my ContentControl to a ContentPresenter otherwise it threw an error. There is detailed explanation on the ContentControl's issue here: http://joshsmithonwpf.wordpress.com/2007/06/28/how-to-use-findname-with-a-contentcontrol/
TigrouMeow