如何防止ListView在Datagrid父级上窃取滚动活动?

时间:2015-07-26 04:37:14

标签: c# wpf listview scroll datagrid

我在DataGridTemplateColumn中有一个ListView,如果鼠标光标位于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>

1 个答案:

答案 0 :(得分:3)

尝试这种方式:

A)更改列表视图ControlTemplate以从中移除ScrollViewer

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

B)另一种方法是在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>