嵌套的AccordionItem。内部AccordionItem不会扩展

时间:2010-04-02 13:29:36

标签: silverlight xaml silverlight-4.0 expression-blend silverlight-toolkit

在Silverlight中,AccordionItem位于另一个内部。当选择内部的那个时,它不能扩展其已经扩展的父级以显示其自己的内容。

我试图通过模板来绕过它,但我很不走运。有没有人有解决方案[更喜欢没有代码的解决方案]?

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:layoutPrimitivesToolkit="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Layout.Toolkit" xmlns:layoutToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit" xmlns:controlsToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
x:Class="NestedAccordion_Silverlight.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot" Background="White">
    <layoutToolkit:Accordion BorderBrush="#FF00FF53" SelectionMode="ZeroOrMore">
        <layoutToolkit:AccordionItem Header="Header" VerticalAlignment="Top" >
            <StackPanel VerticalAlignment="Top">
                <TextBlock TextWrapping="Wrap" Text="Some content"/>
                <Button Content="Button" Width="75"/>
                <layoutToolkit:AccordionItem Header="Inner Accordion1" VerticalAlignment="Top" >
                    <StackPanel VerticalAlignment="Top">
                        <TextBlock TextWrapping="Wrap" Text="Some content"/>
                        <Button Content="Button" Width="75"/>
                    </StackPanel>
                </layoutToolkit:AccordionItem>
            </StackPanel>
        </layoutToolkit:AccordionItem>
        <layoutToolkit:AccordionItem Header="Header" VerticalAlignment="Top" >
            <StackPanel>
                <TextBlock TextWrapping="Wrap" Text="Some content"/>
                <Button Content="Button" Width="75"/>
            </StackPanel>
        </layoutToolkit:AccordionItem>
        <layoutToolkit:AccordionItem Header="Header" VerticalAlignment="Top" >
            <StackPanel>
                <TextBlock TextWrapping="Wrap" Text="Some content"/>
                <Button Content="Button" Width="75"/>
            </StackPanel>
        </layoutToolkit:AccordionItem>
    </layoutToolkit:Accordion>
</Grid>

这是一个错误还是我走错路?

2 个答案:

答案 0 :(得分:1)

试试这个有效。虽然它需要一些代码,但只要它工作顺畅,它就不应该受到太大伤害

[MainPage.xaml中]

<Grid>
    <ScrollViewer Height="500" Width="Auto">
    <layoutToolkit:Accordion SelectedItemsChanged="MainAccordion_SelectedItemsChanged" HorizontalAlignment="Stretch" SelectionMode="ZeroOrOne">
        <layoutToolkit:AccordionItem Header="Main Menu Item 1">
            <layoutToolkit:AccordionItem.Content>
                <StackPanel Orientation="Vertical">
                    <layoutToolkit:Accordion SelectedItemsChanged="InnerAccordion1stLevel_SelectedItemsChanged" HorizontalAlignment="Stretch" SelectionMode="ZeroOrOne">
                        <layoutToolkit:AccordionItem Margin="5,0,0,0" Header="1st Level Child 1">
                            <layoutToolkit:AccordionItem.Content>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 1 - 1" />
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 1 - 2"/>
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 1 - 3"/>
                                    <layoutToolkit:Accordion SelectedItemsChanged="InnerAccordion2ndLevel_SelectedItemsChanged" HorizontalAlignment="Stretch" SelectionMode="ZeroOrOne">
                                        <layoutToolkit:AccordionItem Margin="5,0,0,0" Header="2nd Level Child 1">
                                            <layoutToolkit:AccordionItem.Content>
                                                <StackPanel Orientation="Vertical">
                                                    <TextBlock Margin="15,0,0,0" Text="2nd Level Child 1 - 1" />
                                                    <TextBlock Margin="15,0,0,0" Text="2nd Level Child 1 - 2"/>
                                                    <TextBlock Margin="15,0,0,0" Text="2nd Level Child 1 - 3"/>
                                                    <TextBlock Margin="15,0,0,0" Text="2nd Level Child 1 - 4"/>
                                                </StackPanel>
                                            </layoutToolkit:AccordionItem.Content>
                                        </layoutToolkit:AccordionItem>
                                    </layoutToolkit:Accordion>
                                    <TextBlock Margin="15,0,0,0" Text="Sent Messages"/>
                                </StackPanel>
                            </layoutToolkit:AccordionItem.Content>
                        </layoutToolkit:AccordionItem>
                    </layoutToolkit:Accordion>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 1"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 2"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 3"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 4"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 5"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 6"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 7"/>
                    <layoutToolkit:Accordion SelectedItemsChanged="InnerAccordion1stLevel_SelectedItemsChanged" HorizontalAlignment="Stretch" SelectionMode="ZeroOrOne">
                        <layoutToolkit:AccordionItem Margin="5,0,0,0" Header="1st Level Child 2">
                            <layoutToolkit:AccordionItem.Content>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 2 - 1"/>
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 2 - 2"/>
                                </StackPanel>
                            </layoutToolkit:AccordionItem.Content>
                        </layoutToolkit:AccordionItem>
                    </layoutToolkit:Accordion>
                    <TextBlock Margin="15,0,0,0" Text="Rx Prior Approval"/>
                    <layoutToolkit:Accordion SelectedItemsChanged="InnerAccordion1stLevel_SelectedItemsChanged" HorizontalAlignment="Stretch" SelectionMode="ZeroOrOne">
                        <layoutToolkit:AccordionItem Margin="5,0,0,0" Header="1st Level Child 3">
                            <layoutToolkit:AccordionItem.Content>
                                <StackPanel Orientation="Vertical">
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 3 - 1" />
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 3 - 2"/>
                                    <TextBlock Margin="15,0,0,0" Text="1st Level Child 3 - 3"/>
                                </StackPanel>
                            </layoutToolkit:AccordionItem.Content>
                        </layoutToolkit:AccordionItem>
                    </layoutToolkit:Accordion>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 8"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 9"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 10"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 11"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 12"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 13"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 1 - 14"/>
                    <TextBlock Margin="15,0,0,0" Text="Exit"/>
                </StackPanel>
            </layoutToolkit:AccordionItem.Content>
        </layoutToolkit:AccordionItem>
        <layoutToolkit:AccordionItem Header="Main Menu Item 2">
            <layoutToolkit:AccordionItem.Content>
                <StackPanel Orientation="Vertical">                        
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 1"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 2"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 3"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 4"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 5"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 6"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 7"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 8"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 9"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 10"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 11"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 12"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 13"/>
                    <TextBlock Margin="15,0,0,0" Text="Main Menu Item 2 - 14"/>
                </StackPanel>
            </layoutToolkit:AccordionItem.Content>
        </layoutToolkit:AccordionItem>
    </layoutToolkit:Accordion>
    </ScrollViewer>
</Grid>

在代码背后的几个方法让它工作 [MainPage.xaml.cs中]

    private void MainAccordion_SelectedItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ((Accordion)sender).Height = double.NaN;
    }

    private void InnerAccordion1stLevel_SelectedItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ((Accordion)sender).Height = double.NaN;
        MeasureParentAccordion(sender as Accordion);
    }

    private void InnerAccordion2ndLevel_SelectedItemsChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        ((Accordion)sender).Height = double.NaN;
        MeasureParentAccordion(sender as Accordion);
    }

    private void MeasureParentAccordion(Accordion childAccordion)
    {
        Accordion parentAccordion = ((AccordionItem)((StackPanel)childAccordion.Parent).Parent).Parent as Accordion;
        parentAccordion.Height = parentAccordion.ActualHeight;
        StackPanel accordionItemContent = null;

        foreach (AccordionItem accordionItem in childAccordion.Items)
        {
            if (accordionItem.IsSelected)
            {
                accordionItemContent = accordionItem.Content as StackPanel;
                parentAccordion.Height += accordionItemContent.ActualHeight;

                try
                {
                    Accordion grandParentAccordion = ((AccordionItem)((StackPanel)parentAccordion.Parent).Parent).Parent as Accordion;
                    grandParentAccordion.Height += accordionItemContent.ActualHeight;
                }
                catch
                {
                }
            }
            else
            {
                accordionItemContent = accordionItem.Content as StackPanel;
                parentAccordion.Height -= accordionItemContent.ActualHeight;

                try
                {
                    Accordion grandParentAccordion = ((AccordionItem)((StackPanel)parentAccordion.Parent).Parent).Parent as Accordion;
                    grandParentAccordion.Height -= accordionItemContent.ActualHeight;
                }
                catch
                {
                }
            }
        }
    }

答案 1 :(得分:0)

我认为这是same problem I reported on the CodePlex。当不再需要时,我不得不求助于关闭外部手风琴项目。扩展后,它会调整到新的客户端大小。