Skip to main content

Bind to ICollectionView

  • 3 minutes to read

To bind the GridControl to the ICollectionView, assign the source collection to the grid’s DataControlBase.ItemsSource property and enable the DataControlBase.IsSynchronizedWithCurrentItem option. The GridControl synchronizes its grouping, sorting, current item, and can directly change the underlying collection.

The code sample below binds the GridControl to the ICollectionView and adds buttons to select an item in the collection.

DevExpress WinUI Grid - ICollectionView

<Window
    x:Class="ICollectionView_example.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ICollectionView_example"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:dxg="using:DevExpress.WinUI.Grid"
    mc:Ignorable="d">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <dxg:GridControl AutoGenerateColumns="True" ItemsSource="{x:Bind ViewModel.Orders}" 
                         IsSynchronizedWithCurrentItem="True"/>
        <StackPanel Grid.Row="1" Orientation="Horizontal" Margin="4">
            <Button Content="Previous" Command="{x:Bind ViewModel.MovePrevCommand}"/>
            <Button Content="Next" Command="{x:Bind ViewModel.MoveNextCommand}" Margin="4,0,0,0"/>
        </StackPanel>
    </Grid>
</Window>
using DevExpress.Mvvm;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Data;
using System.Linq;
using System.Windows.Input;

namespace ICollectionView_example {
    public sealed partial class MainWindow : Window {
        public MainWindow() {
            this.InitializeComponent();
        }
        public ViewModel MainViewModel { get; } = new ViewModel();
        public class ViewModel : ViewModelBase {
            public ViewModel() {
                Orders = new CollectionViewSource { Source = OrdersDataModel.GetOrders() }.View;
                MoveNextCommand = new DelegateCommand(() => {
                    if (Orders.CurrentItem == Orders.Last())
                        Orders.MoveCurrentToFirst();
                    else
                        Orders.MoveCurrentToNext();
                });
                MovePrevCommand = new DelegateCommand(() => {
                    if (Orders.CurrentPosition == 0)
                        Orders.MoveCurrentToLast();
                    else
                        Orders.MoveCurrentToPrevious();
                });
            }
            public ICollectionView Orders { get; }
            public ICommand MoveNextCommand { get; }
            public ICommand MovePrevCommand { get; }
        }
    }
}
using System.Collections.ObjectModel;

namespace ICollectionView_example {
    public static class OrdersDataModel {
        public static ObservableCollection<Order> GetOrders() {
            var orders = new ObservableCollection<Order>() {
                new Order() { ProductName = "Chang", UnitPrice = 19, Quantity = 10, Discount = 0, Freight = 30.54 },
                new Order() { ProductName = "Gravad lax", UnitPrice = 12.5, Quantity = 16, Discount = 0, Freight = 21.63 },
                new Order() { ProductName = "Ravioli Angelo", UnitPrice = 19, Quantity = 12, Discount = 0, Freight = 38.94 },
                new Order() { ProductName = "Tarte au sucre", UnitPrice = 22, Quantity = 50, Discount = 0, Freight = 4.45 },
                new Order() { ProductName = "Steeleye Stout", UnitPrice = 18, Quantity = 20, Discount = 0.03, Freight = 33.35 },
                new Order() { ProductName = "Pavlova", UnitPrice = 21, Quantity = 52, Discount = 0, Freight = 14.97 },
                new Order() { ProductName = "Longlife Tofu", UnitPrice = 7.75, Quantity = 120, Discount = 0, Freight = 10.14 },
                new Order() { ProductName = "Alice Mutton", UnitPrice = 21, Quantity = 15, Discount = 0.06, Freight = 18.69 }
            };
            return orders;
        }
    }
    public class Order {
        public string ProductName { get; set; }
        public double UnitPrice { get; set; }
        public int Quantity { get; set; }
        public double Discount { get; set; }
        public double Freight { get; set; }
    }
}