Reputation: 978
I have listboxitems in a list-box that are being populated dynamically from an observable collection and I'm trying to use a Interaction trigger for the 'Selected' event of the listbox item to launch an action so that i can bind to the item coming from the observable collection.
The problem I'm having is that I can seem to access the 'Selected' event with the Event trigger.
Here's the code:
<ListBox x:Name="AssocitedLayerListControl" Background="Transparent" BorderBrush="Transparent" ItemContainerStyle="{StaticResource ListBoxItemStyle}"
ScrollViewer.VerticalScrollBarVisibility="Hidden" ScrollViewer.HorizontalScrollBarVisibility="Auto"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="10,0,10,0" Padding="0" Height="25" Width="Auto" >
<!-- Attribute Table Item Template-->
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0">
<TextBlock Text="{Binding Path=Key}"
Foreground="#FFFFFFFF"
FontSize="10" LineHeight="20"
HorizontalAlignment="Left" VerticalAlignment="Center"
Margin="5,0,5,0" Padding="0" />
<Button BorderBrush="#FFFFFFFF" BorderThickness="1" Background="#FF000000"
HorizontalAlignment="Left" VerticalAlignment="Top"
Padding="1" Margin="0,1,0,1" Style="{StaticResource CloseItemStyle}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<actions:RemoveLayer LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Selected" SourceObject="{Binding RelativeSource={RelativeSource Mode=TemplatedParent, AncestorType=ListBoxItem}}">
<actions:SetGraphicLayerInGrid LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ContentControl>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
Upvotes: 0
Views: 1232
Reputation: 743
Try to listen IsSelected
property of ListBoxItem with DataTrigger :
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
...
<i:Interaction.Triggers>
<ei:DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}" Value="True">
<actions:SetGraphicLayerInGrid LayerName="{Binding Path=Key}" TargetGraphicsLayer="{Binding Path=Value}" />
</ei:DataTrigger>
</i:Interaction.Triggers>
Upvotes: 2