GridView Virtualization in MVVM

3 posts, 0 answers
  1. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 16 Mar Link to this post

        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. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 16 Mar in reply to Vitor Link to this post

    sorry, I messed up. can you please move this to WPF / GridView section?
  3. DevCraft banner
  4. Vitor
    Vitor avatar
    53 posts
    Member since:
    Mar 2016

    Posted 16 Mar Link to this post

    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;
            }
     
        }
    }

     

Back to Top