views:

37

answers:

1

Hi Guys,

In the following XAML, how can I use _xyzStackPanel in the code behind? Is this not possible when we use DataTemplate? I need to show/hide this stackpanel, what is the best way?

Can I use the VisualStateManager here? Could someone provide example please, thanks.

<ListBox  ... >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Name="_xyzStackPanel" ...>
            ..............
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Thanks,

Voodoo

EDIT:

I wonder if it would be better to add the StackPanel in the codebehind instead of always having it there and hiding it......BUT, how can I add to the DataTemplate in the codebehind?

+1  A: 

Bind the Visibility of the StackPanel to the ViewModel object it represents and toggle that instead. You are using MVVM, right??

Paul Betts
No I am not using MVVM.
VoodooChild
@Paul Betts: +1, I ended up binding the visibility to a dummy property of my object...Thanks! I was however surprised to find out that "Name" of the UIElements are not available once inside a DataTemplate....I wonder why?
VoodooChild
@VoodooChild Which StackPanel of the n items in the list would it refer to? Remember, it creates one instance of the DataTemplate per item in the Listbox, so accessing the object directly is ambiguous. The name only makes sense in the context of the DataTemplate, and the DataTemplate has no code-behind.
Paul Betts