试试这个它的工作。虽然它需要一些代码,但只要它运行顺利,它应该不会造成太大的伤害
[主页.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
{
}
}
}
}