Nested AccordionItem. Inner AccordionItem do not expand
In Silverlight an AccordionItem is inside another one . When the inner one is selected, it can not expand its parent more which is already expanded to show its own content.
I tried to get around it by templating but I was unlucky. Does any one has a solution for it [prefer a solution without code]?
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx开发者_StackOverflow/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>
Is it a bug or I am in a wrong path?
Try this it's working. Although it requires a bit of code behind but it shouldn't hurt too much as long as it's working smoorthly
[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>
And in the code behind just a few methods to get it working [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
{
}
}
}
}
I think this is the same problem I reported on the CodePlex. I had to resort to always closing the outer accordion item when it is no longer needed. After expanding, it does adjust to the new client size.
精彩评论