請告訴我如何訪問內部Hubsection * 的DataTemplate *如何訪問任何控制Hubsection的DataTemplate內部在Windows 8.1商店
回答
要訪問任何控制HubSection裏面你可以做這樣的事情:
編輯:爲了使用你必須使用MyToolkit project FindVisualChild擴展方法。你可以下載它作爲一個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指令或程序集引用?) – 2014-05-22 10:56:07
我添加了這些MyToolkit NuGet包,但FindVisualChild返回null ..爲什麼? –
變種秒= testHub.Sections [0]; var gridViewSelect = sec.FindName(「Section4Header」)as GridView;
FindName的伎倆......
在Windows Phone 8.1中返回null – alvinmeimoun
爲Windows Store App 8.1返回null –
在Windows 8.1上返回null – garenyondem
如果你設法已經解決您的問題,我不知道。如果你不在這裏是如何。
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,任何想法爲什麼? – user2469133
答案代碼很好(最小控制樹)。謝謝! –
此代碼片段非常好,謝謝! 我個人修改它有一個返回類型T並說'where T:DependencyObject'。 – Josh
- 1. 代碼點火器商店goCart不訪問任何控制器
- 2. 部署Windows 8.1商店應用程序
- 3. Windows Phone 8.1聯繫商店
- 4. 如何訪問FlipView中的DataTemplate內部的xaml控件
- 5. 如何訪問控制內部的DataRepeater
- 6. 訪問控制8.1
- 7. Windows應用商店8.1文件系統訪問
- 8. 在Windows上部署Windows 8.1商店應用程序10
- 9. WPF 8.1 HubSection
- 10. 如何在Windows 8.1商店應用中獲取Xbox頭像?
- 11. 如何訪問XAML DataTemplate中的控件?
- 12. 在運行時訪問DataTemplate對WPF Toolkit DataGridCell的控制,如何?
- 13. 如何在Win 8.1商店應用程序中訪問HubSections中的控件?在VisualTree中搜索不起作用
- 14. 如何訪問datatemplate內的標籤
- 15. 如何訪問Windows 8商店應用中的IMAP?
- 16. 如何從Windows應用商店中的C++訪問System.Environment?
- 17. 如何在DataTemplate中控制?
- 18. Windows 8.1商店應用程序開發
- 19. 在Windows商店應用程序中訪問Hub Scection的內部元素?
- 20. 如何從Windows 8.1商店應用程序運行.exe文件?
- 21. 如何訪問控制存在一個DataTemplate
- 22. 如何從商店訪問控件中的HTML元素?
- 23. 如何訪問的DataTemplate特定的控制產生列表框
- 24. API來訪問Windows應用商店
- 25. 遷移Windows 8商店的應用程序8.1的SQLite問題
- 26. React Native:HeadslessJS和Redux - 如何從任務訪問商店
- 27. 如何在Windows應用商店應用(Windows 8.1)中設計GridViewItem/GridViewItemPresenter?
- 28. 如何訪問ControlTemplate內部的控件
- 29. DataTemplate中的XAML訪問控制
- 30. metro應用訪問的DataTemplate控制
你嘗試_anything_這麼遠嗎? –
是的......最近我嘗試了這段代碼FlipView fv = GetTemplateChild(「TheFipView」)作爲FlipView; – user3188127
選中此項:[如何訪問XAML DataTemplate中的控件?](http://stackoverflow.com/questions/16375375/how-do-i-access-a-control-inside-a-xaml-datatemplate) – har07