locked
ListView touch input: disable vertical scrolling once Holding event has been raised RRS feed

  • Question

  • Hi,

     I have a scenario where i want to disable vertical scrolling for a listview once the hold event has been raised and re-enable it once the PointerReleased event has fired. 

     <ListView HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
                      ItemsSource="{Binding}" x:Name="lstView" Holding="OnHolding" PointerPressed="OnPointerPressed" PointerReleased="OnPointerReleased">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel Orientation="Vertical" VirtualizingStackPanel.VirtualizationMode="Recycling"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Border Height="80" Background="Olive"
                                HorizontalAlignment="Stretch" Margin="2">
                            <Grid Background="{StaticResource gradientBrush}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height=".4*"/>
                                    <RowDefinition Height=".6*"/>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Title}" FontSize="18" Margin="2" x:Name="txtBlock" Foreground="White" Grid.Row="0"/>
                                <TextBlock x:Name="txtEdit" FontSize="16" Margin="2" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="White"
                                         Text="{Binding Description}" Grid.Row="1" />
                            </Grid>
                        </Border>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

    On the Holding event handler i have set the VirtualizingStackPanel::CanVerticallyScroll and also the ScrollViewer::VerticalScrollMode to None but none of these have any effect.

       private void OnHolding(object sender, Windows.UI.Xaml.Input.HoldingRoutedEventArgs e)
            {
                if (e.HoldingState == Windows.UI.Input.HoldingState.Started)
                {
                    var element = e.OriginalSource as FrameworkElement;
                    var item = element.GetParent<ListViewItem>();
                    if (item != null)
                    {
                        Debug.WriteLine("ListView holding...");
                        _item = item;
                        var transform = _item.RenderTransform as TranslateTransform;
                        if (transform == null)
                        {
                            transform = new TranslateTransform() { X = 10 };
                            _item.RenderTransform = transform;
                        }
    
                        _isHolding = true;
                        _scrollViewer.VerticalScrollMode = ScrollMode.Disabled;
                        _panel.CanVerticallyScroll = false;
                        e.Handled = true;
                    }
                }
            }

    It would have been great to be able to inherit the VirtualizingStackPanel but the class has been sealed. Any help would be much appreciated.

    Thanks.

    Tuesday, August 14, 2012 4:15 PM

Answers

  • An interesting and by-design effect someone else reported as a bug might help you here.  If you put the listview inside a grid with a width set only as much as it needs to allow you to scroll, and then change the width to star width, it will disable scrolling.  Please see this post:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/fd738014-3b6d-4e1a-9bed-7e996a804320


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Wednesday, August 15, 2012 2:30 PM
    Moderator