1
Vote

Button on the item header does not show/hide when the accordion item expands/collapses

description

I am customizing the silverlight accordion control, trying to make a button on the item header show when the accorion item expands, and hide when it collapse. Here how I put the button in the accordion's header template:
 
<layoutToolkit:Accordion x:Name="acc" SelectionMode="ZeroOrOne" DisplayMemberPath="DisplayName" AccordionButtonStyle="{StaticResource AccordionButtonStyle1}" SelectionChanged="acc_SelectionChanged" >
            <layoutToolkit:Accordion.ItemContainerStyle>
                <Style TargetType="layoutToolkit:AccordionItem" >
                    <Setter Property="HeaderTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Width="170" Text="{Binding filterDisplayName}" />
                                    <Button Height="14" Click="Button_Click" Visibility="Collapsed"  />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </layoutToolkit:Accordion.ItemContainerStyle>
</layoutToolkit:Accordion>
 
 
On the accordion selection changed event, I wrote a handler:
 
private void acc_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Accordion ac = sender as Accordion;
        for (int i = 0; i < ac.Items.Count; i++)
        {
            AccordionItem item = ac.ItemContainerGenerator.ContainerFromIndex(i) as AccordionItem;
            StackPanel sp = item.HeaderTemplate.LoadContent() as StackPanel;
            Button refresh = sp.Children[1] as Button;
            if ( acc.SelectedIndex == i)
               refresh.Visibility = Visibility.Visible;
            else
                refresh.Visibility = Visibility.Collapsed;
        }
    }
 
When I debug the code, it goes into the for loop, and recognizes the seleted AccordionItem, locates its header and grabs the button and sets the visibilty to Visible. I can see all these by verifying the local vars. But the button is not showing in reality.

comments