I have not been able to find a clean solution to the following problem even though there are a few related questions already on SO.
If I have a data template that is being used multiple times, for example, a TreeViewItem.HeaderTemplate, how can I change something the template for only some of the TreeViewItems.
For example, let's say my TVI HeaderTemplate has a textblock and depending on the string, I want to make the fontweight bold.
I want to do something like this:
((TextBlock)myTreeView.Items.ElementAt(0).FindName("myTextBlock")).FontWeight = FontWeights.Bold;
Does anyone have a solution for this? --> Thanks Evan
Edit: Is there a way to write a generic function to get a control based on it's name even if it's in a data template?
LayoutRoot.FindName("myTextBlock");
would work if myTextBlock was not in a datatemplate. How can I write a findElementInDataTemplate(string elementName, string parentName)
function?
The reason Evan's answer is not what I'm looking for is because I am developing a control. I want the application developer who uses my control to be able to change any element in the control. If I use Evan's solution, that would require the application developer to have access to all the templates in the control. Possible, but not ideal. Thanks!