Basically, you need to be in the same namespace (read this). Most user interface elements are in the same tree using the same namespace, however, there may be gaps and barriers (styles / templates), and if you have abstract objects like DataGrid columns, they have no scope at all names.
I worked with WPF long enough to guess when I would have problems and I know common areas, but I donโt think there is an easy way to tell in all situations.
If this is true, does this mean that the Binding ElementName should not work in templates at all? But then I definitely have working bindings to the ElementName in my templates.
Inside is just fine, it's the same area. The point here is that if you apply the template and they donโt have their own areas, conflicts will arise.
eg.
<Button/> <Button/>
If you expand the ControlTemplate , you get something like:
<Border Name="bd" Background="{TemplateBinding Background}">...</Border> <Border Name="bd" Background="{TemplateBinding Background}">...</Border>
Obviously, we get a name conflict.
The same goes for DataTemplates in ItemsControls if you specify controls in a template whose name will conflict with the same control instance in the applicable template of other elements.
In another note, you can snap from the inside of the template to the outside, since there can logically be only one instance with this name, or you can give them an excellent priority depending on how to "close" the name area, for example
<TextBox Name="tb" Text="Test"/> <ItemsControl ItemsSource="ABC"> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Text, ElementName=tb}"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl>