问题
Please tell me how to access flipview control inside Hubsection *DataTemplate*
回答1:
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
回答2:
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.
回答3:
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
回答4:
var sec = testHub.Sections[0]; var gridViewSelect = sec.FindName("Section4Header") as GridView;
FindName does the trick...
来源:https://stackoverflow.com/questions/22126659/how-to-access-any-control-inside-hubsection-datatemplate-in-windows-8-1-store