In WPF, is there any real reason to use Storyboard.TargetName="myElement"
instead of Storyboard.Target="{x:Reference myElement}"
?
Asked
Active
Viewed 126 times
1 Answers
0
My answer is based off this post and this MS forum (has examples with pictures)
MS does not comment on this, although from researching I have concluded that ElementName is the way to go as:
- ElementName is optimised for UiElements and won't work for Non-UIElements
- x:Reference returns a reference to the object. ElementName looks for the object in the VisualTree so it is already in memory.
- x:Reference searches a scope (usually top-level window) to search for the element (e.g. Window.UserNameTB) which most likely gives a performance overhead when compared to navigating the VisualTree (cannot say for sure as I did not program that functionality).
{x:Reference ...} ->
- returns just a reference of an object it doesn't create that "bridge" between two properties like binding would do.
- Behind all that a service is being used that searches for the given name in a specific scope which is usually the window itself.
{Binding ElementName="..." } ->
- Creates that binding object then it searches for the object name. The search algorithm moves up and/or down in VisualTree to find the desired element.
- Therefore a functional VisualTree is always needed. As example when used inside a Non-UiElement, it won't work. In the end the Binding stays and does its daily bread.