9

Please tell me how to access flipview control inside Hubsection *DataTemplate*

user3188127
  • 183
  • 1
  • 2
  • 11
  • 1
    Did you try _anything_ so far? – Soner Gönül Mar 02 '14 at 10:58
  • Yes... recently i tried this snippet of code FlipView fv = GetTemplateChild("TheFipView") as FlipView; – user3188127 Mar 02 '14 at 11:24
  • 2
    Check this : [How do I access a control inside a XAML DataTemplate?](http://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate) – har07 Mar 02 '14 at 12:50
  • 2
    @har07 I would like to know as well, I tried the method outlined by Jerry Nixon, but I'm stuck at the foreach loop. For HubSection there are no items to loop through. (i.e. no Items property) But I don't have flipview control, just some items like map, grid etc inside the hubsection – erotavlas Apr 22 '14 at 03:29

4 Answers4

12

I don't know if you managed to solve your problem already. If you didn't here is how.

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;
    }

usage is very simple, for example in my case

ComboBox cb= FindChildControl<ComboBox>(HUB_HC, "SemanaHC") as ComboBox;

Where HUB_HC is my HubSection name and SemanaHC is a combobox inside that HubSection witch is also inside of a StackPanel. It works for me and it's simple to use

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

Community
  • 1
  • 1
Pedro Cavaleiro
  • 835
  • 7
  • 21
  • the combo box i get using this method returns null, any idea why? – user2469133 Oct 31 '14 at 12:09
  • Answer code works great (minimal control tree). Thanks! – Kris Krause Feb 21 '15 at 12:17
  • This code snippet is excellent, thank you! I personally modified it to have a return type of T and say `where T : DependencyObject`. – Josh Jun 22 '15 at 23:37
  • this works to find one element only, but if I am looping inside a ListView and for every ListViewItem I want to access its StackPanel which is in its DataTemplate? I tried looping but i can access only the first stackPanel of the first ListViewItem! Any help? – yalematta May 02 '16 at 17:48
4

The best way to deal with this is to have a user control inside the DataTemplate. And UserControl will have the Flipview, so you can easily access the flipview there.

CodeR
  • 156
  • 1
  • 8
  • but isn't it the same thing? How will you access the `FlipView` in this way? – yalematta May 12 '15 at 13:20
  • Lets say you have Page : HubPage where you have Hub control. Now in one of the sections you have this usercontrol , in that usercontrol you can access the flipview – CodeR May 14 '15 at 18:05
1

To access any control inside a HubSection you can do something like this:

var sec = MyHub.Sections[2];
var btn = sec.FindVisualChild("MyButton") as Button;

EDIT: in order to use FindVisualChild extension method you have to use MyToolkit project. You can download it as a Nuget Package and see the project here.

Hope it helps! :D

EDIT 2: The code for FindVisualChild can be found here: https://mytoolkit.codeplex.com/SourceControl/latest#Shared/UI/FrameworkElementExtensions.cs

Rico Suter
  • 11,548
  • 6
  • 67
  • 93
robertohuertasm
  • 846
  • 9
  • 17
  • 2
    'Windows.UI.Xaml.Controls.HubSection' does not contain a definition for 'FindVisualChild' and no extension method 'FindVisualChild' accepting a first argument of type 'Windows.UI.Xaml.Controls.HubSection' could be found (are you missing a using directive or an assembly reference?) –  May 22 '14 at 10:56
  • I added these MyToolkit NuGet packages but FindVisualChild returns null.. Why? – v.g. Mar 27 '15 at 15:59
-1

var sec = testHub.Sections[0]; var gridViewSelect = sec.FindName("Section4Header") as GridView;

FindName does the trick...