ListView and pointer events - any way to handle this?

不打扰是莪最后的温柔 提交于 2019-12-22 08:35:41

问题


This is quite old and I believe common case - I have a ListView and I want to let it handle vertical scroll, but horizontal movement of pointer I would like to handle myself.

There is a good blog post from Rob Caplan, but it's already three years old and I don't know if there had anything changed in W10. In the post, at the end, Rob suggests to avoid scrolling the ListView manually from own event. Is there any better, easier way to handle this?

Sample code can look like this:

<Grid x:Name="mainGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView HorizontalAlignment="Stretch" Background="Transparent" VerticalAlignment="Stretch">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="Background" Value="Green"/>
            </Style>
        </ListView.ItemContainerStyle>
        <x:String>First Item</x:String>
        <x:String>Second Item</x:String>
        <x:String>Third Item</x:String>
        <x:String>Fourth Item</x:String>
    </ListView>
</Grid>

code behind:

public MainPage()
{
    this.InitializeComponent();
    mainGrid.PointerMoved += (sender ,e) => Debug.WriteLine("Pointer moved"); 
    mainGrid.AddHandler(PointerPressedEvent, new PointerEventHandler((sender, e) => Debug.WriteLine("Pointer pressed")), true);
    mainGrid.AddHandler(PointerReleasedEvent, new PointerEventHandler((sender, e) => Debug.WriteLine("Pointer released")), true);
}

The main problem is that when ListView's scrollviewer get pointer event it 'swallows' it. As you can see above we are able to subscribe to pressed/released events so that they gets called anyway, but it's not possible for pointermoved event. Theoretically there should be a way to achieve this, hence for example ListView inside Pivot works just fine - vertical scroll is performed by ListView and horizontal by Pivot. How this can be done? Any ideas?

来源:https://stackoverflow.com/questions/36737422/listview-and-pointer-events-any-way-to-handle-this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!