How to access a Control inside the data template in C# Metro UI in the code behind

后端 未结 3 546
心在旅途
心在旅途 2020-11-30 13:56

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

相关标签:
3条回答
  • 2020-11-30 14:23

    Try the following:

        private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
        {
            int childNumber = VisualTreeHelper.GetChildrenCount(control);
            for (int i = 0; i < childNumber; i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(control, i);
                FrameworkElement fe = child as FrameworkElement;
                // Not a framework element or is null
                if (fe == null) return null;
    
                if (child is T && fe.Name == ctrlName)
                {
                    // Found the control so return
                    return child;
                }
                else
                {
                    // Not found it - search children
                    DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
                    if (nextLevel != null)
                        return nextLevel;
                }
            }
            return null;
        }
    

    Then call it from the play / pause button button:

        MediaElement media = FindChildControl<MediaElement>(this, "media") as MediaElement;
        media.Play();
    

    A related blog post on the subject

    0 讨论(0)
  • 2020-11-30 14:34

    I wrote a blog article on this very topic about a year ago. Perhaps it will help you: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

    The gist is this. You must parse through the visual tree to get all elements, and then you can use something like LINQ to filter the results and get your objects.

    0 讨论(0)
  • 2020-11-30 14:41

    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<Control> AllChildren(DependencyObject parent, Func<DependencyObject, bool> query,   List<Control> _List = null ) 
        {
            if (_List == null)
                 _List = new List<Control>();
    
            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;
        }
    
    0 讨论(0)
提交回复
热议问题