Search code examples
c#wpfxamlxceedxceed-datagrid

Xceed DataGridCollectionViewSource with Sample Data Source


Using Xceed DataGrid for WPF

How can you use generated sample data source (generated in Expression Blend) as the source for DataGridCollectionViewSource? Is it possible?

    <xcdg:DataGridCollectionViewSource x:Key="cvsSample"
                                     Source="{Binding Source={x:Static Application.Current},Path=SampleDataSource}"/>

Doing this throw an error:

A value of type 'DataGridCollectionViewSource' cannot be added to a collection or dictionary of type 'UIElementCollection'.

I can set it directly in the DataGridControl like so:

    <xcdg:DataGridControl ItemTemplate="{DynamicResource ItemTemplate}" 
                      ItemsSource="{Binding Collection, Source={StaticResource SampleDataSource}}"
                      UpdateSourceTrigger="CellContentChanged"
                      Margin="10">
    </xcdg:DataGridControl>

But I want to use the DataGridCollectionViewSource as it allows you to use the filtering, grouping etc. functionality.


Solution

  • Try this:

    enter image description here

    XAML:

    <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:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
            xmlns:local="clr-namespace:WpfApp1"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <xcdg:DataGridCollectionViewSource x:Key="cvsSample" Source="{Binding}" />
        </Window.Resources>
        <Grid>
            <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvsSample}}"/>
        </Grid>
    </Window>
    

    CS:

    using Xceed.Wpf.Samples.SampleData;
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = SampleDataProvider.GetProducts();
        }
    }