1

在 Silverlight 中,AccordionItem 在另一个 AccordionItem 内。When the inner one is selected, it can not expand its parent more which is already expanded to show its own content.

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

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

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

4

2 回答 2

1

试试这个它的工作。虽然它需要一些代码,但只要它运行顺利,它应该不会造成太大的伤害

[主页.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
                {
                }
            }
        }
    }
于 2011-05-05T13:57:42.350 回答
0

我认为这与我在 CodePlex 上报告的问题相同。当不再需要时,我不得不总是关闭外部手风琴项目。扩展后,它会适应新的客户端大小。

于 2010-04-04T18:02:10.800 回答