0
点赞
收藏
分享

微信扫一扫

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

JakietYu 06-13 19:00 阅读 1

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

相关推荐

0 条评论