简体   繁体   English

Xceed DataGridCollectionViewSource与示例数据源

[英]Xceed DataGridCollectionViewSource with Sample Data Source

Using Xceed DataGrid for WPF 将Xceed DataGrid用于WPF

How can you use generated sample data source (generated in Expression Blend) as the source for DataGridCollectionViewSource? 如何使用生成的样本数据源(在Expression Blend中生成)作为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'. 无法将类型'DataGridCollectionViewSource'的值添加到'UIElementCollection'类型的集合或字典中。

I can set it directly in the DataGridControl like so: 我可以像这样直接在DataGridControl中进行设置:

    <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. 但是我想使用DataGridCollectionViewSource,因为它允许您使用过滤,分组等功能。

Try this: 尝试这个:

在此处输入图片说明

XAML: 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: CS:

using Xceed.Wpf.Samples.SampleData;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = SampleDataProvider.GetProducts();
    }
}

Look at jstreet's answer, but if that doesnt work for you, you can try doing what I did. 查看jstreet的答案,但是,如果这对您不起作用,您可以尝试做我所做的事情。

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). 在Visual Studio中,转到“ 项目”>“添加引用”>“扩展”,然后添加Xceed.Wpf.DataGrid.Samples.SampleData (请记住选中旁边的小框)。

App.xaml.cs 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 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... 不能相信我为错过参考而付出了很多努力...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM