Expander Width Problem
-
I'm trying to create a ListBox, with it's ItemTemplate being an expander, which itself has a list in it. Here's a pic of what I'm trying to accomplish. [Pic](https://1drv.ms/u/s!AlkRTpT49yCMmgxTy4tnD-TMpXa1?e=nEvqc0) I added colors around each item to highlight what's happening. Here's the XAML
<ListBox Grid.Row="2"
Grid.Column="1"
x:Name="outerList"
Margin="2,2,2,2"
Background="Transparent"
ItemsSource="{Binding RecentItemSections}"
SelectedItem="{Binding SelectedRecentItemSection}"
HorizontalAlignment="Stretch"
BorderBrush="Yellow"
BorderThickness="2"><ListBox.ItemTemplate> <DataTemplate> <Expander IsExpanded="{Binding IsSectionExpanded}" x:Name="expander" HorizontalAlignment="Stretch" BorderBrush="Red" BorderThickness="2"> <Expander.Header> <TextBlock Text="{Binding SectionName}" Foreground="White" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=ActualWidth}"/> </Expander.Header> <ListBox ItemsSource="{Binding RecentItems}" BorderBrush="Green" BorderThickness="5" Margin="2,0,20,0"> <ListBox.ItemTemplate> <DataTemplate> <Border BorderBrush="MediumAquamarine" BorderThickness="2"> <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"> <TextBlock Text="{Binding ItemName}" FontSize="12" Margin="2"/> <TextBlock Text="{Binding ItemLocation}" FontSize="10" Margin="2,0,2,2"/> </StackPanel> </Border>
-
I'm trying to create a ListBox, with it's ItemTemplate being an expander, which itself has a list in it. Here's a pic of what I'm trying to accomplish. [Pic](https://1drv.ms/u/s!AlkRTpT49yCMmgxTy4tnD-TMpXa1?e=nEvqc0) I added colors around each item to highlight what's happening. Here's the XAML
<ListBox Grid.Row="2"
Grid.Column="1"
x:Name="outerList"
Margin="2,2,2,2"
Background="Transparent"
ItemsSource="{Binding RecentItemSections}"
SelectedItem="{Binding SelectedRecentItemSection}"
HorizontalAlignment="Stretch"
BorderBrush="Yellow"
BorderThickness="2"><ListBox.ItemTemplate> <DataTemplate> <Expander IsExpanded="{Binding IsSectionExpanded}" x:Name="expander" HorizontalAlignment="Stretch" BorderBrush="Red" BorderThickness="2"> <Expander.Header> <TextBlock Text="{Binding SectionName}" Foreground="White" Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=ActualWidth}"/> </Expander.Header> <ListBox ItemsSource="{Binding RecentItems}" BorderBrush="Green" BorderThickness="5" Margin="2,0,20,0"> <ListBox.ItemTemplate> <DataTemplate> <Border BorderBrush="MediumAquamarine" BorderThickness="2"> <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch"> <TextBlock Text="{Binding ItemName}" FontSize="12" Margin="2"/> <TextBlock Text="{Binding ItemLocation}" FontSize="10" Margin="2,0,2,2"/> </StackPanel> </Border>
You should know what the maximum header width will be; set the .Width accordingly. (The framework doesn't know beforehand).
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I