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
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
.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:
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.