listbox有scrollviewer,点击后不会触发selectionchanged事件

这个问题是因为在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>

    <ListBox
        x: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>
                <Border
                    Margin="0"
                    BorderBrush="Red"
                    BorderThickness="2,2,2,2"
                    CornerRadius="3">
                    <StackPanel Margin="2,0,0,0">
                       
                        <StackPanel>
                            <ScrollViewer
                                xmlns: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>
                                <TextBlock
                                    Width="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>

最近更新

  1. TCP协议是安全的吗?

    2024-06-06 17:52:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-06 17:52:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-06 17:52:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-06 17:52:06       18 阅读

热门阅读

  1. C语言---指针part2

    2024-06-06 17:52:06       8 阅读
  2. jvm 触发GC的时机和条件

    2024-06-06 17:52:06       6 阅读
  3. 分析JVM堆Dump日志定位线程阻塞原因

    2024-06-06 17:52:06       10 阅读
  4. 进口单座调节阀的特点

    2024-06-06 17:52:06       10 阅读
  5. 二叉树的右视图-力扣

    2024-06-06 17:52:06       7 阅读
  6. python脚本将视频抽帧为图像数据集

    2024-06-06 17:52:06       9 阅读
  7. Golang获取文件名扩展名/后缀

    2024-06-06 17:52:06       8 阅读
  8. Nginx的负载均衡(加权轮询)

    2024-06-06 17:52:06       7 阅读
  9. 【Power Compiler手册】6.反标翻转活动

    2024-06-06 17:52:06       9 阅读
  10. C++基础-编程练习题和答案(数组)

    2024-06-06 17:52:06       8 阅读