2014-03-02 63 views
9

請告訴我如何訪問內部Hubsection * 的DataTemplate *如何訪問任何控制Hubsection的DataTemplate內部在Windows 8.1商店

+1

你嘗試_anything_這麼遠嗎? –

+0

是的......最近我嘗試了這段代碼FlipView fv = GetTemplateChild(「TheFipView」)作爲FlipView; – user3188127

+2

選中此項:[如何訪問XAML DataTemplate中的控件?](http://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate) – har07

回答

4

flipview控制來處理這種情況的最好方法是讓內部用戶控制DataTemplate。 並且UserControl將具有Flipview,因此您可以輕鬆訪問那裏的翻轉視圖。

+0

但是不是它是一回事嗎?你將如何以這種方式訪問​​FlipView? – yalematta

+0

假設你有Hub:你有Hub控件的頁面:HubPage。現在在這個用戶控件的其中一個部分中,在該用戶控件中,您可以訪問flipview – CodeR

1

要訪問任何控制HubSection裏面你可以做這樣的事情:

​​

編輯:爲了使用你必須使用MyToolkit project FindVisualChild擴展方法。你可以下載它作爲一個Nuget包,並看到項目here

希望它有幫助! :d

編輯2:用於FindVisualChild的代碼可以在這裏找到:https://mytoolkit.codeplex.com/SourceControl/latest#Shared/UI/FrameworkElementExtensions.cs

+2

'Windows.UI.Xaml.Controls.HubSection'不包含'FindVisualChild'的定義,也沒有包含接受第一個'FindVisualChild'的擴展方法可以找到'Windows.UI.Xaml.Controls.HubSection'類型的參數(你是否缺少using指令或程序集引用?) – 2014-05-22 10:56:07

+0

我添加了這些MyToolkit NuGet包,但FindVisualChild返回null ..爲什麼? –

-1

變種秒= testHub.Sections [0]; var gridViewSelect = sec.FindName(「Section4Header」)as GridView;

FindName的伎倆......

+0

在Windows Phone 8.1中返回null – alvinmeimoun

+0

爲Windows Store App 8.1返回null –

+0

在Windows 8.1上返回null – garenyondem

12

如果你設法已經解決您的問題,我不知道。如果你不在這裏是如何。

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

+0

我使用這個方法得到的組合框返回null,任何想法爲什麼? – user2469133

+0

答案代碼很好(最小控制樹)。謝謝! –

+0

此代碼片段非常好,謝謝! 我個人修改它有一個返回類型T並說'where T:DependencyObject'。 – Josh

相關問題