如何在代码后面获取ListBox ItemsPanel
我有一个带有 ItemsPanel 的 ListBox
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel x:Name="ThumbListStack" Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
,我想使用后面代码中的 TranslateTransform 沿 X 轴移动堆栈面板。
问题是,我找不到堆栈面板。
ThumbListBox.FindName("ThumbListStack")
什么也不返回。 我想将它用于:
Storyboard.SetTarget(x, ThumbListBox.FindName("ThumbListStack"))
如何获取堆栈面板,以便我可以将它与 TranslateTransform 一起使用
谢谢
I have a ListBox with an ItemsPanel
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<StackPanel x:Name="ThumbListStack" Orientation="Horizontal" />
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
I am wanting to move the Stack Panel along the X-axis using a TranslateTransform in code behind.
Problem is, I can't find the Stack Panel.
ThumbListBox.FindName("ThumbListStack")
Returns nothing.
I want to use it in:
Storyboard.SetTarget(x, ThumbListBox.FindName("ThumbListStack"))
How do I get the Stack Panel so I can then use it with the TranslateTransform
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
ItemsPanelTemplate
中的StackPanel
的Loaded
事件然后在代码后面
另一种方法是遍历可视化树并找到
StackPanel
,它将成为ItemsPresenter
的第一个子项。You can use the
Loaded
event for theStackPanel
that is in theItemsPanelTemplate
And then in code behind
Another way is to traverse the Visual Tree and find the
StackPanel
which will be the first child of theItemsPresenter
.抱歉,我刚刚注意到我忘记保存编辑...我意识到您已经接受了答案,但对我来说这似乎更像是一种黑客行为。这是我对 FindChild 的实现,您可能希望在将来使用它,或者如果您打算经常这样做。
它检查所有子项以及子项的子项,比较控件上设置的类型和名称。像这样使用它:
sorry I just noticed I forgot to save the edit...I realize you've already accepted an answer, but it seems more of a hack to me. Here's my implementation of FindChild, you might want to use it for the future or if you're going to be doing this often.
It checks all the children and the children's children comparing the type and Name set on the control. Use it like this:
尝试以下扩展方法:
方法本身:
PS: 您可以自己扩展它以检查特定的控件名称,以便方法将返回单个控件而不是列表。
Try out following extension method:
Method itself:
PS: You can yourself extend it to check for specific control name so method would return single control instead of list.