这个问题是因为在ListBox中的ScrollViewer处于焦点状态时,SelectionChanged事件没有触发。这通常是因为ScrollViewer在处理鼠标事件时会优先于ListBox。
为了解决这个问题,可以通过以下方法来确保SelectionChanged事件在ListBox中被触发:
在ListBox的ItemContainerStyle中添加一个触发器,当ListBoxItem得到焦点时,设置其IsSelected属性为True
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsSelected" Value="True"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<!-- ListBox items here -->
</ListBox>
<ListBoxx:Name="FaultInfoList"Width="220"ItemsSource="{Binding RealTimeFaultList}"><ListBox.ItemContainerStyle><Style TargetType="ListBoxItem"><Style.Triggers><Trigger Property="IsMouseOver" Value="True"><Setter Property="IsSelected" Value="True" /></Trigger></Style.Triggers></Style></ListBox.ItemContainerStyle><i:Interaction.Triggers><i:EventTrigger EventName="SelectionChanged"><i:InvokeCommandAction Command="{Binding DataContext.OpenDeviceDetailViewCommand, ElementName=mainWindow}" CommandParameter="{Binding ElementName=FaultInfoList, Path=SelectedItem}" /></i:EventTrigger></i:Interaction.Triggers><ItemsControl.ItemTemplate><DataTemplate><BorderMargin="0"BorderBrush="Red"BorderThickness="2,2,2,2"CornerRadius="3"><StackPanel Margin="2,0,0,0"><StackPanel><ScrollViewerxmlns:sys="clr-namespace:System;assembly=mscorlib"Height="70"Panel.ZIndex="-2"BorderThickness="0"VerticalScrollBarVisibility="Auto"><ScrollViewer.Resources><sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">4</sys:Double><sys:Double x:Key="{x:Static SystemParameters.HorizontalScrollBarHeightKey}">4</sys:Double></ScrollViewer.Resources><TextBlockWidth="175"Margin="10,10,10,10"Text="{Binding Current_fault}"TextWrapping="Wrap"><i:Interaction.Triggers><i:EventTrigger EventName="MouseEnter"><i:InvokeCommandAction Command="{Binding DataContext.FaultDetailTextBlockMouseEnterCommand, ElementName=mainWindow}" CommandParameter="{Binding}" /></i:EventTrigger><i:EventTrigger EventName="MouseLeave"><i:InvokeCommandAction Command="{Binding DataContext.FaultDetailTextBlockMouseLeaveCommand, ElementName=mainWindow}" CommandParameter="{Binding}" /></i:EventTrigger></i:Interaction.Triggers></TextBlock></ScrollViewer></StackPanel></Border></DataTemplate></ItemsControl.ItemTemplate></ListBox>