Xceed DataGridCollectionViewSource样本数据来源
使用Xceed DataGrid的WPF的Xceed DataGridCollectionViewSource样本数据来源
如何使用生成的样本数据源(Expression Blend中生成)为源DataGridCollectionViewSource?可能吗?
<xcdg:DataGridCollectionViewSource x:Key="cvsSample"
Source="{Binding Source={x:Static Application.Current},Path=SampleDataSource}"/>
这样做会引发错误:
A value of type 'DataGridCollectionViewSource' cannot be added to a collection or dictionary of type 'UIElementCollection'.
我可以在DataGridControl直接设置它,像这样:
<xcdg:DataGridControl ItemTemplate="{DynamicResource ItemTemplate}"
ItemsSource="{Binding Collection, Source={StaticResource SampleDataSource}}"
UpdateSourceTrigger="CellContentChanged"
Margin="10">
</xcdg:DataGridControl>
但我想用DataGridCollectionViewSource,因为它可以让你使用过滤,分组等功能。
试试这个:
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();
}
}
看起来不错,但你从哪里得到的 “SampleDataProvider” ?我有Xceed.Products.Wpf.DataGrid.Full,它不被识别。我试图寻找某种样品包,但似乎找不到任何。 我也尝试使用_Xceed.Wpf.DataGrid.Samples_,我发现其中一个示例项目,但它表示_Samples_不存在于命名空间_Xceed.Wpf.DataGrid_中。我错过了什么?我无法找到它.. –
好吧,我终于把它工作了!我没有像你那样完全做,但如果你能找到SampleDataProvider,你的解决方案看起来很棒。我意识到我没有添加对Xceed.Wpf.DataGrid.Samples.SampleData的引用(请记住检查它旁边的小框)。我会为任何可能需要它的人发布代码。 谢谢! :) –
@HjalteTagmose:你非常欢迎!但请记住:我们在这里说**的方式是通过投票回答有用的答案:O)看看[这里](https://*.com/help/someone-answers)。 – jsanalytics
看jstreet的答案,但如果不适合你,你可以尝试做我所做的。
在Visual Studio中去项目>添加引用>扩展并添加Xceed.Wpf.DataGrid.Samples.SampleData(记得下次检查的小盒子的话)。
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>
不能相信我挣扎这么多只是错过了一个参考......
你在哪里定义cvsSample资源?并且Application.Current.SampleDataSource返回什么?什么类型? – mm8
cvsSample是在DataGridControl上定义的,但现在我把它放在UserControl.Resources中,这似乎修复了错误,但datagrid不再显示数据。SampleDataSource是xaml生成的。它包含xaml/xsd。我猜(对不起noob)它返回的XAML,因为这是所有的项目被存储的地方。 XAML看起来是这样的: \t \t'' –