-1

There is a scroll viewer that contains a list view and there is list view on the sroll viewr. and when scroll on the list view it doesn't scroll but when you scroll out the list view(on the area of scroll viewer that is not behind list view).How can I fix this? My code:

 <ScrollViewer Background="#111" HorizontalScrollBarVisibility="Disabled" FlowDirection="LeftToRight" Grid.Row="1" >

            <Grid Grid.Row="1">
                <StackPanel   Background="#111" HorizontalAlignment="Center">

                    <ListView  BorderThickness="2" HorizontalAlignment="Right" ItemsSource="{Binding MyList}">
                     
                            //My codes

                    </ListView>



                </StackPanel>
            </Grid>

   </ScrollViewer>
Eboy
  • 61
  • 7
  • Related: [XAML/WPF - ScrollViewer which has StackPanel inside is not scrolling](https://stackoverflow.com/questions/45802078/xaml-wpf-scrollviewer-which-has-stackpanel-inside-is-not-scrolling) – mcalex Dec 21 '21 at 06:30
  • I didn't know that. That was helpful and now it's working fine. I've never thought that the problem could be the stack panel. thanks:) – Eboy Dec 22 '21 at 08:52

1 Answers1

0

A ScrollViewer enables content to be displayed in a smaller area than its actual size. For scrolling all elements, you could try to refer to the following code.

<Window x:Class="ScrollDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ScrollDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <ScrollViewer>
        <Grid Height="500">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition Height="100"/>
            </Grid.RowDefinitions>
            <TextBlock Text="scroll"/>
            <ScrollViewer  Background="#111" HorizontalScrollBarVisibility="Disabled" FlowDirection="LeftToRight" Grid.Row="1" >
                <ListView  x:Name="lv" BorderThickness="2" HorizontalAlignment="Right" ItemsSource="{Binding }">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Width="100" Header="Name" DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Width="100" Header="Age" DisplayMemberBinding="{Binding Age}"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </ScrollViewer>
        </Grid>
    </ScrollViewer>
</Window>

MainWindow.xaml.cs:

using System.Collections.Generic;
using System.Windows;
namespace ScrollDemo
{
  public partial class MainWindow : Window
  {
    List<Item> MyList;
    public MainWindow()
    {
      InitializeComponent();
      MyList = new List<Item>();
      for (int i = 0; i < 10; i++)
      {
        MyList.Add(new Item(){  Name= $"list{ i }",Age=i });
      }
      lv.ItemsSource=MyList;
    }
  }
  public class Item
  {
    public string Name { get;set;}
    public int Age { get;set;}
  }
}

The result

LSQ
  • 52
  • 9
  • Thanks for your answering. That wasn't the thing that I was really looking for but that helps me a lot to fix my bug. – Eboy Dec 22 '21 at 08:54