如何防止ListView窃取数据网格父节点上的滚动活动

本文关键字:父节点 滚动 活动 网格 数据网 ListView 何防止 数据 | 更新日期: 2023-09-27 18:04:55

我有一个ListView在一个datagridtemplatecoluml,如果鼠标光标在ListView中的一个项目上,并且用户试图滚动DataGrid, DataGrid不滚动。

我已经尝试了以下操作,但它仍然发生:

<ListView ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Disabled" ...

编辑:提供下面的例子,我使用ListView使项目在溢出时换行。

<DataGridTemplateColumn Header="Name" Width="120" ScrollViewer.IsDeferredScrollingEnabled="True">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ListView ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Disabled"  ScrollViewer.VerticalScrollBarVisibility="Disabled" VerticalAlignment="Center" ItemsSource="{Binding DataList}">
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Width="{Binding (FrameworkElement.ActualWidth), RelativeSource={RelativeSource AncestorType=ScrollContentPresenter}}" ItemWidth="{Binding (ListView.View).ItemWidth, RelativeSource={RelativeSource AncestorType=ListView}}" ItemHeight="{Binding (ListView.View).ItemHeight, RelativeSource={RelativeSource AncestorType=ListView}}"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock>
                            <Hyperlink TextDecorations="{x:Null}" NavigateUri="{Binding Path=.}" RequestNavigate="Name_RequestNavigate">
                                <TextBlock Padding="1" Text="{Binding Path=., Converter={StaticResource NameToLocale}}"/>
                            </Hyperlink>
                        </TextBlock>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

如何防止ListView窃取数据网格父节点上的滚动活动

试试这样做:

A)更改列表视图ControlTemplate以删除ScrollViewer:

<ListView>
    <ListView.Template>
        <ControlTemplate>
            <ItemsPresenter/>
        </ControlTemplate>
    </ListView.Template>
    ...
</ListView>

B)另一种方法是基于这个答案为父元素创建一个行为

// Used on sub-controls of an expander to bubble the mouse wheel scroll event up 
public sealed class BubbleScrollEvent : Behavior<UIElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewMouseWheel += AssociatedObject_PreviewMouseWheel;
    }
    protected override void OnDetaching()
    {
        AssociatedObject.PreviewMouseWheel -= AssociatedObject_PreviewMouseWheel;
        base.OnDetaching();
    }
    void AssociatedObject_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
    {
        e.Handled = true;
        var e2 = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
        e2.RoutedEvent = UIElement.MouseWheelEvent;
        AssociatedObject.RaiseEvent(e2);
    }
}
<SomePanel>
            <i:Interaction.Behaviors>
                <viewsCommon:BubbleScrollEvent />
            </i:Interaction.Behaviors>
</SomePanel>