1

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.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
Hjalte Tagmose
  • 67
  • 1
  • 12
  • Where are you defining the cvsSample resource? And what does Application.Current.SampleDataSource return? What's the type? – mm8 Aug 29 '17 at 12:32
  • cvsSample was defined right over the DataGridControl, but I put it in UserControl.Resources now, which seems to fix the error, but the datagrid doesnt show the data anymore.. The SampleDataSource is xaml is generated. It contains xaml/xsd. I'm guessing(sorry noob) it's returning the xaml as that's where all the items are stored. xaml looks like this: `` – Hjalte Tagmose Aug 29 '17 at 13:01

2 Answers2

1

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();
    }
}
jsanalytics
  • 13,058
  • 4
  • 22
  • 43
  • Looks good, but where did you get the "SampleDataProvider"? I've got the Xceed.Products.Wpf.DataGrid.Full, and it isn't being recognized. I tried looking for some kind of sample package, but cant seem to find any. I also tried using _Xceed.Wpf.DataGrid.Samples_, which I found in one of the sample projects, but it says that _Samples_ does not exist in the namespace _Xceed.Wpf.DataGrid_. What am I missing? I cant find it.. – Hjalte Tagmose Aug 31 '17 at 07:20
  • Okay, I finally got it to work! I didn't exactly do it like you, but your solution looks like a great one, if you can find the SampleDataProvider. I realized I hadn't added a reference to Xceed.Wpf.DataGrid.Samples.SampleData (remember to check the little box next to it). I'll post the code below for anyone, who might need it. Thank you! :) – Hjalte Tagmose Aug 31 '17 at 07:43
  • @HjalteTagmose : You're very welcome! But remember: the way we say **thank you** here at SO is by voting up helpful answers :O) Take a look [HERE](https://stackoverflow.com/help/someone-answers). – jsanalytics Aug 31 '17 at 08:02
  • Got a little carried away with the coding, sorry. You got it now :) – Hjalte Tagmose Aug 31 '17 at 08:36
1

Look at jstreet's answer, but if that doesnt work for you, you can try doing what I did.

In Visual Studio go to Project > Add Reference > Extensions and add Xceed.Wpf.DataGrid.Samples.SampleData (remember to check the little box next to it).

App.xaml.cs

    public partial class App : System.Windows.Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        Xceed.Wpf.DataGrid.Licenser.LicenseKey = "XXXXX-XXXXX-XXXXX-XXXX";

        DataSet musicDataSet = Xceed.Wpf.DataGrid.Samples.SampleData.DataProvider.GetMusicLibraryDataSet();
        m_songs = musicDataSet.Tables["Songs"];

        base.OnStartup(e);
    }

    private DataTable m_songs;

    public DataTable Songs
    {
        get
        {
            return m_songs;
        }
    }
}

MainWindow.xaml

<Window.Resources>
    <xcdg:DataGridCollectionViewSource x:Key="cvsSongs"
                                     Source="{Binding Source={x:Static Application.Current},Path=Songs}">
    </xcdg:DataGridCollectionViewSource>
</Window.Resources>

<Grid>
    <xcdg:DataGridControl ItemsSource="{Binding Source={StaticResource cvsSongs}}"/>
</Grid>

Can't believe I struggled this much just to have missed a reference...

Hjalte Tagmose
  • 67
  • 1
  • 12