Home > Blockchain >  How to make scrollbar over all of the elements wpf?
How to make scrollbar over all of the elements wpf?

Time:12-22

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>

CodePudding user response:

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

  • Related