简体   繁体   中英

ScrollViewer.ChangeView() does not scroll horizontally

I'm trying to create two scrollviewers that scroll vertically and horizontally respectively.

The scrolling should start when the manipulationdelta event is detected, and I have used the ChangeView method to move the scrollviewers accordingly. The vertical scrollviewer is ok, but it's not the same for the horizontal one. I have added an image to show the 2 scrollviewers since i cannot post it right here.

Here's the code: XAML

   <!--horizontal scrollviewer--> 
   <ScrollViewer x:Name="horizontalScrollViewer" 
                  Grid.Column="1"
                  Grid.RowSpan="3"
                  HorizontalScrollMode="Disabled"
                  VerticalScrollMode="Disabled">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="80"/>
                <RowDefinition Height="100"/>
                <RowDefinition Height="80"/>
            </Grid.RowDefinitions>
            <!--upperGrid-->
            <GridView Grid.Row="0"
                      ScrollViewer.HorizontalScrollMode="Disabled"
                      ScrollViewer.VerticalScrollMode="Disabled"
                      ItemsSource="{Binding dataItems}">
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Vertical"
                                           MaximumRowsOrColumns="1"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Height="80" Width="80" Text="{Binding}"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
            <!--bottomGrid-->
            <GridView Grid.Row="2"
                      ScrollViewer.HorizontalScrollMode="Disabled"
                      ScrollViewer.VerticalScrollMode="Disabled"
                      ItemsSource="{Binding dataItems}">
                <GridView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <VariableSizedWrapGrid Orientation="Vertical"
                                           MaximumRowsOrColumns="1"/>
                    </ItemsPanelTemplate>
                </GridView.ItemsPanel>
                <GridView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Height="80" Width="80" Text="{Binding}"/>
                    </DataTemplate>
                </GridView.ItemTemplate>
            </GridView>
            
        </Grid>
    </ScrollViewer>
    

and the .cs

    private void dataGrid_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
         var x = e.Delta.Translation.X;
        var y = e.Delta.Translation.Y;
        if(Math.Abs(x)> Math.Abs(y))
        {

            bool result = horizontalScrollViewer.ChangeView( horizontalScrollViewer.HorizontalOffset - x , null, null);
            result = centralScrollViewer.ChangeView(horizontalScrollViewer.HorizontalOffset - x, null, null);
            
        }
        else
        {
            bool result = centralScrollViewer.ChangeView(null, verticalScrollViewer.VerticalOffset -y, null);
            verticalScrollViewer.ChangeView(null, verticalScrollViewer.VerticalOffset - y, null);
                            
        }
    }

In the msdn documentation I have read that :

horizontalOffset Type: IReference [C++] | System.Nullable [.NET] A value between 0 and ScrollableWidth that specifies the distance the content >should be scrolled horizontally.

but my ScrollableWidth is equal to zero and I can't understand why.

Does anyone know what's wrong with my code?

I've found a solution by myself!

I had to add the horizontalscrollbar visibility option on the horizontal scrollviewer. It's quite odd since I didn't need to add it to the vertical one. Anyway here's the code of the horizontal scrollviewer:

<ScrollViewer x:Name="horizontalScrollViewer" 
                  Grid.RowSpan="3"
                  Grid.Column="1"
                  HorizontalScrollMode="Disabled"
                  HorizontalScrollBarVisibility="Auto"
                  VerticalScrollMode="Disabled">

I don't think you can scroll while scrolling is disabled. If you're just trying to hide the scrollbars, then do the following:

<ScrollViewer x:Name="horizontalScrollViewer" 
                  Grid.Column="1"
                  Grid.RowSpan="3"
                  HorizontalScrollMode="Enabled"
                  VerticalScrollMode="Enabled"
                  HorizontalScrollBarVisibility="Hidden"
                  VerticalScrollBarVisibility="Hidden">
...
</ScrollViewer>

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