我在列表框中有listboxitems,它们是从一个可观察的集合中动态填充的,我正在尝试使用一个Interaction触发器对列表框项的'Selected'事件启动一个动作,以便我可以绑定到来自可观察集合的项目。
我遇到的问题是我似乎可以通过事件触发器访问“已选择”事件。
以下是代码:
<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>
答案 0 :(得分:2)
尝试使用DataTrigger侦听ListBoxItem的IsSelected
属性:
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>