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

GridView Virtualization in MVVM

2 Answers 86 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vitor
Top achievements
Rank 1
Vitor asked on 16 Mar 2016, 09:38 AM

    Hey Telerik!

to achieve a virtualization on a gridview (infinite scrolling) without the MVVM I followed this doc:

http://docs.telerik.com/devtools/wpf/controls/radgridview/populating-with-data/Populating-DataVirtualization

works like a charm.

but my attempt to have it working using a viewmodel doesn't. The list comes empty


What is the best way to have a virtualization on a gridview, while maintaining MVVM pattern?


Thank you for your help!

 

here is my code:

MainWindow.xaml

<Window x:Class="TelerikVirtualization.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                Title="MainWindow" Height="900" Width="700"
                xmlns:my="clr-namespace:TelerikVirtualization" >
 
    <Window.Resources>
        <my:ViewModel x:Key="ViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource ViewModel}">
        <telerik:RadGridView ItemsSource="{Binding View}" >
               
        </telerik:RadGridView>
    </Grid>
</Window>

 

MainWindow.xaml.cs

using System.Windows;
namespace TelerikVirtualization
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

 

ViewModel.cs (if it is possible, I would like to move the GetList() to a Load() method. so that I can handle the Window_Loaded event to Load() it, thus not having to load it every time this class gets initialized. but for now it is fine like this)

using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
 
namespace TelerikVirtualization
{
    [ImplementPropertyChanged] //install Fody.PropertyChanged nugget for this
    public class ViewModel
    {
 
        public VirtualQueryableCollectionView View;
        Controller controller;
        public ViewModel()
        {
            controller = new Controller();
            View = new VirtualQueryableCollectionView(controller.GetList()) { LoadSize = 10 };
 
        }
    }
}

 

Controller.cs

using System.Linq;
 
namespace TelerikVirtualization
{
    public class Controller
    {
        public IQueryable GetList()
        {
            return new DataClasses1DataContext().MyTables;
        }
    }
}

 

2 Answers, 1 is accepted

Sort by
0
Vitor
Top achievements
Rank 1
answered on 16 Mar 2016, 09:38 AM
sorry, I messed up. can you please move this to WPF / GridView section?
0
Vitor
Top achievements
Rank 1
answered on 16 Mar 2016, 10:48 AM

Well I made it work using a code so similar to one I posted above

I will share it to whoever might come to need it

 

MainWindow.xaml

<Window x:Class="TelerikVirtualization.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                Title="MainWindow" Height="900" Width="700" >
 
    <Grid>
        <telerik:RadGridView ItemsSource="{Binding View}" FilteringMode="FilterRow">
        </telerik:RadGridView>
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
namespace TelerikVirtualization
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new ViewModel();
        }
           
    }
}

ViewModel.cs

using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Telerik.Windows.Data;
 
namespace TelerikVirtualization
{
    [ImplementPropertyChanged]
    public class ViewModel
    {
        public VirtualQueryableCollectionView View { get; set; }
        Controller controller;
        public ViewModel()
        {
            controller = new Controller();
            View = new VirtualQueryableCollectionView(controller.GetList()) { LoadSize = 10 };
        }
    }
}

Controller.cs

using System.Linq;
 
namespace TelerikVirtualization
{
    public class Controller
    {
        public IQueryable GetList()
        {
            return new DataClasses1DataContext().MyTables;
        }
 
    }
}

 

Tags
GridView
Asked by
Vitor
Top achievements
Rank 1
Answers by
Vitor
Top achievements
Rank 1
Share this question
or