This is a migrated thread and some comments may be shown as answers.

CanContentScroll not working

1 Answer 577 Views
GridView
This is a migrated thread and some comments may be shown as answers.
yoochul
Top achievements
Rank 1
yoochul asked on 25 Oct 2018, 05:24 AM

Hi,

I want radgridview to scroll like Datagid  (ScrollViewer.CanContentScroll="True")

 

<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <DataGrid ItemsSource="{Binding SampleDataList}"  ScrollViewer.CanContentScroll="True" Grid.Row="0" RowHeight="40" />
        <telerik:RadGridView  ItemsSource="{Binding SampleDataList}" Grid.Row="1" ScrollViewer.CanContentScroll="True" RowHeight="40"  />
    </Grid>
</Window>

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 29 Oct 2018, 10:41 AM
Hello yoochul,

Because there's no way to guarantee that all of the rows of the control will have the same height (due to custom cell templates, row details, hierarchy etc.), RadGridView does not support per item scrolling out of the box.

If you're certain that all your items will have the same height - specified by the RowHeight property - you can use a similar approach to handle the scrolling:

private GridViewScrollViewer scrollViewer;
 
public MainWindow()
{
    InitializeComponent();
    this.Grid.Loaded += Grid_Loaded;
}
 
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
    scrollViewer = this.Grid.ChildrenOfType<GridViewScrollViewer>().First();
    scrollViewer.ScrollChanged += ScrollViewer_ScrollChanged;
}
 
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
    var delta = (this.Grid.RowHeight - scrollViewer.VerticalOffset) % this.Grid.RowHeight;
    var coef = e.VerticalChange < 0 ? -1 : 1;
    scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + (delta * coef));
}

Of course, you may need to fine tune this to work in all scenarios but it should give you a general idea of how to handle the process.

I do hope you find such an approach applicable.

Regards,
Dilyan Traykov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
yoochul
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or