将鼠标悬停在内部列表视图上时,列表视图中的 WPF 列表视图不会滚动

本文关键字:列表 视图 WPF 滚动 悬停 鼠标 在内部 | 更新日期: 2023-09-27 18:37:01

>我在列表视图中有一个列表视图,当鼠标悬停在内部列表视图上时,外部列表视图停止滚动。
当鼠标悬停在外部 ListView 中的项上以及鼠标悬停在滚动条上时,滚动工作。

XAML:

<Window x:Class="ListViewInsideListViewNoteScrolling.CheckForUpdatesView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="CheckForUpdatesView" Height="300" Width="500">
    <Grid>
        <ListView Grid.Row="0" Margin="20 10" BorderThickness="0" BorderBrush="Transparent"
                          HorizontalAlignment="Stretch" 
                          ScrollViewer.VerticalScrollBarVisibility="Auto" 
                          ScrollViewer.CanContentScroll="False"
                          ItemsSource="{Binding VersionsDetails}" 
                          ItemContainerStyle="{StaticResource CheckForUpdatesListView}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="400">
                        <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                        </Grid.RowDefinitions>
                        <TextBlock Grid.Row="0" FontWeight="Bold">
                                        <Run Text="Version "/> <Run Text="{Binding Number}"/>
                        </TextBlock>
                        <TextBlock Grid.Row="1" Text="{Binding Description}" TextWrapping="Wrap"/>
                        <ListView Grid.Row="2" Margin="10 0 0 0" BorderThickness="0" BorderBrush="Transparent"
                                          ItemsSource="{Binding Details}"
                                          ItemContainerStyle="{StaticResource CheckForUpdatesListView}">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <BulletDecorator Margin="4">
                                        <BulletDecorator.Bullet>
                                            <Ellipse Height="5" Width="5" Fill="Black"/>
                                        </BulletDecorator.Bullet>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding}" Width="350"></TextBlock>
                                    </BulletDecorator>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
                        <!--<TextBlock Grid.Row="3" Text="{Binding Details, Converter={StaticResource ListToTextConverter}}" TextWrapping="Wrap"/>-->
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>
</Window>

风格:

  <Style x:Key="CheckForUpdatesListView" TargetType="ListViewItem">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="OverridesDefaultStyle" Value="true"/>
        <Setter Property="Focusable" Value="False"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" Padding="0" BorderThickness="0" SnapsToDevicePixels="true" Background="Transparent">
                        <ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Transparent"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

VersionsDetails是一个List<VersionInfo>

public class VersionInfo
{
    public string Number { get; set; }
    public string Description { get; set; }
    public List<string> Details { get; set; }
}

一种可能的解决方案是,而不是内部列表视图,使用此行

<TextBlock Grid.Row="3" Text="{Binding Details, Converter={StaticResource ListToTextConverter}}" TextWrapping="Wrap"/>

这会将我的列表转换为项目符号字符串,但 ListView 的样式更好。我也愿意接受使用其他 Wpf 控件/元素来解决此问题的建议。

将鼠标悬停在内部列表视图上时,列表视图中的 WPF 列表视图不会滚动

一个不错的解决方案是创建一种忽略鼠标滚轮的行为。

1> 导入系统.Windows.交互性

2> 创建行为:

 public sealed class IgnoreMouseWheelBehavior : Behavior<UIElement>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.PreviewMouseWheel += AssociatedObjectPreviewMouseWheel;
        }
        protected override void OnDetaching()
        {
            AssociatedObject.PreviewMouseWheel -= AssociatedObjectPreviewMouseWheel;
            base.OnDetaching();
        }
        private void AssociatedObjectPreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            e.Handled = true;
            var mouseWheelEventArgs = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
                     {
                         RoutedEvent = UIElement.MouseWheelEvent
                     };
            AssociatedObject.RaiseEvent(mouseWheelEventArgs);
        }

3> 将行为附加到内部列表视图

                <ListView Grid.Row="2" Margin="10 0 0 0" BorderThickness="0"
                                  ItemsSource="{Binding Details}"
                                  ItemContainerStyle="{StaticResource CheckForUpdatesListView}">
                            <i:Interaction.Behaviors>
                                <local:IgnoreMouseWheelBehavior />
                            </i:Interaction.Behaviors>
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <BulletDecorator Margin="4">
                                        <BulletDecorator.Bullet>
                                            <Ellipse Height="5" Width="5" Fill="Black"/>
                                        </BulletDecorator.Bullet>
                                        <TextBlock TextWrapping="Wrap" Text="{Binding}" Width="350"></TextBlock>
                                    </BulletDecorator>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                </ListView>                        

灵感来自这个紧密的问题:冒泡滚动事件从列表视图到其父级