【问题标题】:How to access any control inside Hubsection Datatemplate in Windows 8.1 store如何在 Windows 8.1 商店中访问 Hubsection Datatemplate 中的任何控件
【发布时间】:2014-04-03 07:32:49
【问题描述】:

请告诉我如何访问 Hubsection 内的翻转视图控件 *DataTemplate*

【问题讨论】:

  • 到目前为止,您尝试过任何东西吗?
  • 是的...最近我尝试了代码 FlipView fv = GetTemplateChild("TheFipView") as FlipView 的 sn-p;
  • @har07 我也想知道,我尝试了 Jerry Nixon 概述的方法,但我被困在 foreach 循环中。对于 HubSection,没有要循环的项目。 (即没有 Items 属性)但我没有翻转视图控件,只有 hubsection 内的一些项目,如地图、网格等

标签: c# xaml windows-store-apps datatemplate windows-8.1


【解决方案1】:

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

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

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

希望对您有所帮助! :D

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

【讨论】:

  • “Windows.UI.Xaml.Controls.HubSection”不包含“FindVisualChild”的定义,并且没有扩展方法“FindVisualChild”接受“Windows.UI.Xaml.Controls”类型的第一个参数。可以找到 HubSection'(您是否缺少 using 指令或程序集引用?)
  • 我添加了这些 MyToolkit NuGet 包,但 FindVisualChild 返回 null.. 为什么?
【解决方案2】:

我不知道您是否已经设法解决了您的问题。如果你不在这里是怎么回事。

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

用法很简单,比如我的例子

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

HUB_HC 是我的 HubSection 名称,SemanaHC 是 HubSection 女巫内部的组合框,它也在 StackPanel 内部。它对我有用,而且使用起来很简单

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

【讨论】:

  • 我使用此方法得到的组合框返回 null,知道为什么吗?
  • 答案代码效果很好(最小控制树)。谢谢!
  • 这段代码sn-p很好,谢谢!我个人将其修改为返回类型为 T 并说where T : DependencyObject
  • 这只能找到一个元素,但是如果我在 ListView 内循环并且对于每个 ListViewItem 我想访问其 DataTemplate 中的 StackPanel?我尝试循环,但我只能访问第一个 ListViewItem 的第一个 stackPanel!有什么帮助吗?
【解决方案3】:

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

FindName 可以解决问题...

【讨论】:

  • 在 Windows Phone 8.1 中返回 null
  • 为 Windows Store App 8.1 返回 null
  • 在 Windows 8.1 上返回 null
【解决方案4】:

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

【讨论】:

  • 但这不是一回事吗?你将如何通过这种方式访问​​FlipView
  • 假设你有 Page : HubPage 你有 Hub 控制。现在在您拥有此用户控件的部分之一中,您可以在该用户控件中访问翻转视图
猜你喜欢
  • 2017-03-13
  • 2015-04-27
  • 2020-08-31
  • 2014-12-04
  • 2013-04-28
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多