How to access any control inside Hubsection Datatemplate in Windows 8.1 store
Asked Answered
S

4

9

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

Shebeen answered 2/3, 2014 at 10:53 Comment(4)
Did you try anything so far?Felishafelita
Yes... recently i tried this snippet of code FlipView fv = GetTemplateChild("TheFipView") as FlipView;Shebeen
Check this : How do I access a control inside a XAML DataTemplate?Ruella
@Ruella 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 hubsectionAquinas
K
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

Ka answered 23/9, 2014 at 15:48 Comment(4)
the combo box i get using this method returns null, any idea why?Functionary
Answer code works great (minimal control tree). Thanks!Chorography
This code snippet is excellent, thank you! I personally modified it to have a return type of T and say where T : DependencyObject.Assamese
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?Division
G
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.

Gardas answered 3/3, 2014 at 17:49 Comment(2)
but isn't it the same thing? How will you access the FlipView in this way?Division
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 flipviewGardas
I
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

Implore answered 22/4, 2014 at 22:33 Comment(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?)Unexpressive
I added these MyToolkit NuGet packages but FindVisualChild returns null.. Why?Cellaret
G
-1

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

FindName does the trick...

Gatias answered 8/8, 2014 at 22:20 Comment(3)
Return null in Windows Phone 8.1Saucer
Returns null for Windows Store App 8.1Chorography
Returns null on Windows 8.1Petasus

© 2022 - 2024 — McMap. All rights reserved.