I have a MediaElement that resides inside the datatemplate of flipview, i want to access that MediaElement named \"video\" in the code behind so that i can assign properties
I extended Jerry's solution to a more flexible and better performance solution in getting only desired controls and do not create intermediated Lists during recursive calls.
You can simply using like that to get the control:
var myControl = AllChildren(parent, c => c.Name == "xxx").FirstOrDefault();
For that you should include the following AllChildren funcion:
private List AllChildren(DependencyObject parent, Func query, List _List = null )
{
if (_List == null)
_List = new List();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control && query(_Child))
{
_List.Add(_Child as Control);
}
AllChildren(_Child, query, _List);
}
return _List;
}