C# 如何在Windows 8.1应用商店中访问Hubsection Datatemplate中的任何控件
请告诉我如何访问Hubsection中的flipview控件*DataTemplate*处理此问题的最佳方法是在DataTemplate中使用用户控件。C# 如何在Windows 8.1应用商店中访问Hubsection Datatemplate中的任何控件,c#,xaml,windows-store-apps,datatemplate,windows-8.1,C#,Xaml,Windows Store Apps,Datatemplate,Windows 8.1,请告诉我如何访问Hubsection中的flipview控件*DataTemplate*处理此问题的最佳方法是在DataTemplate中使用用户控件。 用户控件将具有Flipview,因此您可以轻松访问其中的Flipview。要访问HubSection中的任何控件,您可以执行以下操作: var sec = MyHub.Sections[2]; var btn = sec.FindVisualChild("MyButton") as Button; 编辑:为了使用FindVisualChild
用户控件将具有Flipview,因此您可以轻松访问其中的Flipview。要访问HubSection中的任何控件,您可以执行以下操作:
var sec = MyHub.Sections[2];
var btn = sec.FindVisualChild("MyButton") as Button;
编辑:为了使用FindVisualChild扩展方法,您必须使用。您可以将其作为Nuget包下载并查看该项目
希望有帮助!
:D
编辑2:FindVisualChild的代码可以在这里找到:var sec=testHub.Sections[0]; var gridViewSelect=sec.FindName(“Section4Header”)作为GridView
FindName起作用了…我不知道你是否已经解决了你的问题。如果你不在这里,这是怎么回事
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;
}
私有DependencyObject FindChildControl(DependencyObject控件,字符串ctrlName)
{
int childNumber=VisualTreeHelper.GetChildrenCount(控件);
for(int i=0;i
用法非常简单,例如在我的例子中
ComboBox cb= FindChildControl<ComboBox>(HUB_HC, "SemanaHC") as ComboBox;
组合框cb=FindChildControl(HUB_HC,“SemanaHC”)作为组合框;
其中HUB_HC是我的HubSection名称,SemanaHC是该HubSection内的一个组合框,该组合框也位于StackPanel内。它适合我,而且使用简单
参考资料:到目前为止,您尝试过什么吗?是的。。。最近,我尝试将这段代码FlipView fv=GetTemplateChild(“TheFipView”)作为FlipView;检查这个:@har07我也想知道,我尝试了杰瑞·尼克松概述的方法,但我被困在了foreach循环中。对于HubSection,没有要循环的项。(即no Items属性)但我没有flipview控件,只有hubsection的Windows.UI.Xaml.Controls.hubsection中的一些项,如map、grid等,它不包含“FindVisualChild”的定义,并且找不到接受“Windows.UI.Xaml.Controls.hubsection”类型的第一个参数的扩展方法“FindVisualChild”(您是否缺少using指令或程序集引用?)我添加了这些MyToolkit NuGet软件包,但FindVisualChild返回null..为什么?Windows Phone 8.1Returns null Windows应用商店应用程序中返回null。Windows 8.1使用此方法获得的组合框返回null,知道原因吗?答案代码非常有效(最小控制树)。谢谢!这个代码段非常棒,谢谢!我个人修改它的返回类型为T,并说
其中T:DependencyObject
。这只用于查找一个元素,但是如果我在ListView中循环,并且对于每个ListViewItem,我想访问其DataTemplate中的StackPanel?我尝试了循环,但我可以访问这只是第一个ListViewItem的第一个stackPanel!有任何帮助吗?但这不是同一件事吗?您将如何以这种方式访问FlipView
?假设您有Page:hubbage,其中有Hub控件。现在,在其中一个部分中,您有此usercontrol,在该usercontrol中,您可以访问FlipView