访问 ListBoxItem 的子项
我有一个带有 DataTemplate 的列表框,如下所示:
<ListBox Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate DataType="x:Type local:NumericIconDefinition">
<Grid>
<ComboBox Name="IconComboBox"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我想获取 ComboBox 实例 以便进行操作它在后面的代码中。我发现一篇博客文章解释了获取 ListBoxItem 的过程:
ListBoxItem lbi = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromIndex(IndexInListBox);
但我找不到访问该项目中的 Grid 和 ComboBox 实例的好方法。理想情况下,基于上面的代码,我想做这样的事情:
ComboBox cb = (ComboBox)lbi.GetChildByName("IconComboBox");
I have a ListBox with a DataTemplate that looks like this:
<ListBox Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate DataType="x:Type local:NumericIconDefinition">
<Grid>
<ComboBox Name="IconComboBox"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I would like to fetch the ComboBox instance in order to manipulate it in the code behind. I found a blog post that explained the process of fetching the ListBoxItem:
ListBoxItem lbi = (ListBoxItem)listBox.ItemContainerGenerator.ContainerFromIndex(IndexInListBox);
But I cant find a good way to access the Grid and then ComboBox instances in that item. Ideally, building upon the code above, I would like to do something like this:
ComboBox cb = (ComboBox)lbi.GetChildByName("IconComboBox");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过模板的 FindName 方法访问它:
请注意,只有在 ListBoxItem 完全加载后才能执行此操作,否则模板将不会被实例化
You can access it though the FindName method of the template :
Note that you can only do that after the ListBoxItem is fully loaded, otherwise the template won't be instantiated yet