Find a WPF element inside DataTemplate in the code-behind

I use this function a lot in my WPF programs to find children elements: public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) yield return (T)child; foreach … Read more

Stop TabControl from recreating its children

By default, the TabControl shares a panel to render it’s content. To do what you want (and many other WPF developers), you need to extend TabControl like so: TabControlEx.cs [TemplatePart(Name = “PART_ItemsHolder”, Type = typeof(Panel))] public class TabControlEx : TabControl { private Panel ItemsHolderPanel = null; public TabControlEx() : base() { // This is necessary … Read more

Access parent DataContext from DataTemplate

I had problems with the relative source in Silverlight. After searching and reading I did not find a suitable solution without using some additional Binding library. But, here is another approach for gaining access to the parent DataContext by directly referencing an element of which you know the data context. It uses Binding ElementName and … Read more

tech