【问题标题】:WPF ListView inside ListView not scrolling when mouse over the inner ListView当鼠标悬停在内部 ListView 上时,ListView 内的 WPF ListView 不滚动
【发布时间】:2016-03-01 10:12:46
【问题描述】:

我在 ListView 中有一个 ListView,当鼠标悬停在内部 ListView 上时,外部 ListView 停止滚动。
当鼠标悬停在外部 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>

VersionsDetailsList&lt;VersionInfo&gt;

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

一个可能的解决方案是,而不是内部 ListView,使用这一行

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

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

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

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

    1> 导入 System.Windows.Interactivity

    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

                    <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>                        
    

    受到这个问题的启发:Bubbling scroll events from a ListView to its parent

    【讨论】:

    • 您需要添加以下引用:System.WindowsSystem.Windows.ControlsSystem.Windows.Input,您可能需要获取 Blend SDK NuGet 包,并找到并引用 System.Windows.Interactivity程序集扩展部分中的 DLL。 i 命名空间在 XAML 中声明为 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"xmlns:i="clr-namespace:System.Windows.Interactivity;assembly‌​=System.Windows.Inte‌​ractivity"local 声明为 xmlns:local="clr-namespace:YourNamespace"
    • 对“xmlns”定义的更正:上一条评论的命名空间在 2018 年未找到,但这个替代定义可以完成工作:xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"(它来自 DLL 参考 System.Windows.Interactivity。 )
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多