如何访问Windows 8.1存储中的Hubsection Datatemplate内的任何控件

请告诉我如何在Hubsection * DataTemplate *中访问flipview控件

我不知道你是否已经设法解决了你的问题。 如果你没有在这里是如何。

private DependencyObject FindChildControl(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(child, ctrlName); if (nextLevel != null) return nextLevel; } } return null; } 

用法非常简单,例如在我的情况下

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

其中HUB_HC是我的HubSection名称,而SemanaHC是一个combobox,其内部HubSection女巫也在StackPanel内。 它适用于我,它使用简单

参考: 如何在后面的代码中访问C#Metro UI中的数据模板内的Control

处理此问题的最佳方法是在DataTemplate中使用用户控件。 UserControl将具有Flipview,因此您可以轻松访问那里的flipview。

要访问HubSection内的任何控件,您可以执行以下操作:

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

编辑:为了使用FindVisualChild扩展方法,您必须使用MyToolkit项目 。 您可以将其作为Nuget包下载并在此处查看项目。

希望能帮助到你! :d

编辑2:FindVisualChild的代码可以在这里找到: https ://mytoolkit.codeplex.com/SourceControl/latest#Shared/UI/FrameworkElementExtensions.cs

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

FindName诀窍……