Вложенный Аккордеонный Элемент. Внутренний AccordionItem не расширяется - PullRequest
1 голос
/ 02 апреля 2010

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

1 голос
/ 05 мая 2011

Попробуйте, это работает. Хотя для этого требуется немного кода, но он не должен причинять слишком много вреда, если он работает без сбоев

[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
                {
                }
            }
        }
    }
0 голосов
/ 04 апреля 2010

Я думаю, что это та же проблема, о которой я сообщал на CodePlex . Мне приходилось прибегать к закрытию внешнего предмета гармошки, когда он больше не нужен. После расширения он подстраивается под новый размер клиента.

...