Reputation: 5681
I've got a ListBox
with ListBoxItem
using a DataTemplate
that uses Expander
as its container. The problem is that Expander
appears to be eating up Click
event (HeaderSite
part of Expander
to be exact) and I never get the SelectedItem
if I click on Expander
(but it works if you click on ListBoxItem
itself).
Any idea on how to get Expander
to play nicely with ListBox
?
Here's a simplified Xaml that will reproduce the problem (no code behind needed):
Edit Code updated to bring closer to my actual template but screenshots are still from previous revision (the problem is same - this is just to clarify problem with first answer)
<Window x:Class="ListBoxSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<TextBlock>
<Run Text="Selected Item" />
<Run Text="{Binding ElementName=ListBox, Path=SelectedItem}" />
</TextBlock>
<ListBox x:Name="ListBox">
<ListBoxItem>
<Expander Header="Expandable Stuff 1">
<ListBox>
<ListBoxItem>1.1</ListBoxItem>
<ListBoxItem>1.2</ListBoxItem>
</ListBox>
</Expander>
</ListBoxItem>
<ListBoxItem>
<Expander Header="Expandable Stuff 2">
<ListBox>
<ListBoxItem>2.1</ListBoxItem>
<ListBoxItem>2.2</ListBoxItem>
</ListBox>
</Expander>
</ListBoxItem>
</ListBox>
</StackPanel>
</Window>
Screenshots are pre-edit
Clicking on ListBoxItem
resulting a SelectedItem
:
Clicking on Expander resulting in no SelectedItem
update (click was on Expander 1 as evident by dashed outline):
Upvotes: 21
Views: 4623
Reputation: 2422
Well the following code seems to work with the disadvantage (or maybe advantage) that at each time only Selected item will be expnaded.
Apply the following 2 attributes to your 2 Expander
s
IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}"
By binding IsHitTestVisible
as well, it allows the elements contained within the Expander
to be interacted with.
Resulting in:
<ListBox x:Name="ListBox" IsSynchronizedWithCurrentItem="True">
<ListBoxItem>
<Expander Header="Expandable Stuff 1" IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
1
</Expander>
</ListBoxItem>
<ListBoxItem>
<Expander Header="Expandable Stuff 2" IsHitTestVisible="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
2
</Expander>
</ListBoxItem>
</ListBox>
ANother solution with code behind would be as such:
<ListBox x:Name="ListBox" IsSynchronizedWithCurrentItem="True">
<ListBoxItem>
<Expander Header="Expandable Stuff 1" IsHitTestVisible="False" IsExpanded="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}">
<StackPanel IsHitTestVisible="True">
<Label Content="1"/>
</StackPanel>
</Expander>
</ListBoxItem>
<ListBoxItem>
<Expander Header="Expandable Stuff 2" ButtonBase.Click="Expander_Click_1" Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}">
2
</Expander>
</ListBoxItem>
</ListBox>
And code behind:
private void Expander_Click_1(object sender, RoutedEventArgs e)
{
if (sender is Expander)
{
Expander senderExp = (Expander)sender;
object obj = senderExp.Tag;
if (obj is ListBoxItem)
{
((ListBoxItem)obj).IsSelected = true;
}
}
}
Upvotes: 7
Reputation: 17083
Without code behind you could do this
<ListBox.ItemContainerStyle>
<Style>
<Style.Triggers>
<EventTrigger RoutedEvent="Control.PreviewMouseLeftButtonDown">
<BeginStoryboard>
<Storyboard Storyboard.TargetProperty="(Selector.IsSelected)">
<BooleanAnimationUsingKeyFrames Duration="0:0:0">
<DiscreteBooleanKeyFrame Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
Upvotes: 13