C#学习教程:如何在代码中获取列表框项目面板问题是,我找不到堆栈面板。ThumbListBox.FindName("ThumbListStack")什么都不返回我想使用这个:Storyboard.SetTarget(x,ThumbListBox.FindName("ThumbListStack"))我怎样才能得到堆栈面板,这样我就可以将它与TranslateTransform一起使用谢谢你能帮忙吗StackPanel中的ItemsPanelTemplate使用Loaded事件,然后在私有StackPanel后面的代码中使用m_itemsPanelStackPanel;privatevoidStackPanel_Loaded(objectsender,RoutedEventArgse){m_itemsPanelStackPanel=senderasStackPanel;另一种方法是遍历VisualTree并找到StackPanel,这将是ItemsPresenter的第一个子ItemsPresenter。publicvoidSomeMethod(){ItemsPresenteritemsPresenter=GetVisualChild(listBox);StackPanelitemsPanelStackPanel=GetVisualChild(itemsPresenter);}privatestaticTGetVisualChild(DependencyObjectparent)whereT:Visual{Tchild=default(T);(父母);for(inti=0;i(v);}if(child!=null){break;}}returnchild;}抱歉,我刚刚注意到我忘了保存编辑...我看到你接受了回答,但这对我来说更像是一个hack。这是我对FindChild的实现,您将来可能想使用它,或者如果您经常这样做的话。publicstaticTFindChild(thisFrameworkElementobj,stringname){DependencyObjectdep=objasDependencyObject;Tret=default(T);if(dep!=null){intchildcount=VisualTreeHelper.GetChildrenCount(dep);for(inti=0;i(name);if(ret!=null)break;}}returnret;}它检查所有孩子和孩子的孩子,比较控件上设置的类型和名称。像这样使用它:StackPanelipanel=ThumbListBox.FindChild("ThumbListStack");if(ipanel==null)MessageBox.Show("找不到任何东西");elseMessageBox.Show("啊哈!发现:"ipanel.Name);试试这个扩展方法:varchildStackPanels=FindVisualChildren(ThumbListBox);方法本身:publicstaticIEnumerableFindVisualChildren(thisDependencyObjectdepObj)whereT:DependencyObject{if(depObj!=null){for(inti=0;i(child)){yieldreturnchildOfChild;}}}}PS:您可以自己扩展它以检查特定的控件名称,因此该方法将返回单个控件而不是列表。以上就是C#学习教程:代码中如何获取ListBoxItemsPanel共享的所有内容。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。侵权请点击右侧联系管理员删除。如需转载请注明出处:
