简体   繁体   中英

How to prevent ListView from stealing scroll activity on Datagrid parent?

I have a ListView within a DataGridTemplateColumn, if the mouse cursor is over an item within the ListView and the user attempts to scroll the DataGrid, the DataGrid doesn't scroll.

I have tried the following but it still occurs:

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

Edit: Providing example below of where this is used, I'm using ListView to make the items wrap on overflow.

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

Try this ways:

A) Change list view ControlTemplate to remove ScrollViewer from it:

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

B) Another way is create a behavior for parent element base on this answer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM