TigrouMeow
TigrouMeow

Reputation: 3799

With WPF, how to retrieve the controls contained by a DataTemplate?

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?

Upvotes: 0

Views: 469

Answers (2)

HackerBaloo
HackerBaloo

Reputation: 537

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

Upvotes: 1

Natrium
Natrium

Reputation: 31204

Try FindResource()

here's an example

Upvotes: 0

Related Questions