Skip to main content

Binding to ICollectionView

  • 3 minutes to read

The Grid Control supports ICollectionView binding. If the DataControlBase.IsSynchronizedWithCurrentItem property is set to true, the grid automatically synchronizes its currently selected item with the underlying collection.

Example

The example shows how to bind the Grid Control to the ICollectionView.

Use buttons to select an item in the collection. A checkbox specifies whether the grid’s currently selected item is synchronized with the collection.

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:ICollectionView_binding"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Grid="using:DevExpress.UI.Xaml.Grid"
    x:Class="ICollectionView_binding.MainPage"
    mc:Ignorable="d">
    <Grid>
        <Grid:GridControl Name="grid" AutoGenerateColumns="True" ShowFixedTotalSummary="True" ItemsSource="{Binding Data}" Margin="0,0,0,40" IsSynchronizedWithCurrentItem="{Binding ElementName=checkBox, Path=IsChecked}" >
            <Grid:GridControl.TotalSummary>
                <Grid:GridSummaryItem x:Name="item" SummaryType="Count" Alignment="Right" />
            </Grid:GridControl.TotalSummary>
        </Grid:GridControl>
        <Button VerticalAlignment="Bottom" HorizontalAlignment="Left" Content="Previous" Click="Button_Left" />
        <Button VerticalAlignment="Bottom" HorizontalAlignment="Right" Content="Next" Click="Button_Right" />
        <CheckBox VerticalAlignment="Bottom" HorizontalAlignment="Center" Content="IsSynchronizedWithCurrentItem" Name="checkBox" />
    </Grid>
</Page>
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;

namespace ICollectionView_binding
{
    public sealed partial class MainPage : Page
    {
        DataModel grid_data = new DataModel();
        public MainPage()
        {
            this.DataContext = grid_data;
            this.InitializeComponent();

        }

        public class DataModel {
            public ICollectionView Data { get; set; }
            public DataModel() {
                Data = new CollectionViewSource {
                    Source = CreateList()
                }.View;
            }
        }

        public static IList CreateList() {
            List<TestData> list = new List<TestData>();
            for (int i = 0; i < 100; i++) {
                list.Add(new TestData() {
                    Number1 = i,
                    Number2 = i * 10,
                    Text1 = "row " + i,
                    Text2 = "ROW " + i
                });
            }
            return list;
        }
        public class TestData {
            public int Number1 { get; set; }
            public int Number2 { get; set; }
            public string Text1 { get; set; }
            public string Text2 { get; set; }
        }

        private void Button_Left(object sender, RoutedEventArgs e) {
            if (grid_data.Data.CurrentPosition == 0)
                grid_data.Data.MoveCurrentToLast();
            else
                grid_data.Data.MoveCurrentToPrevious();
        }

        private void Button_Right(object sender, RoutedEventArgs e) {
            if (grid_data.Data.CurrentItem == grid_data.Data.Last())
                grid_data.Data.MoveCurrentToFirst();
            else
                grid_data.Data.MoveCurrentToNext();
        }
    }
}