如何在数据表绑定到C#和XAML一个WPF数据网格 [英] How to bind a table in a dataset to a WPF datagrid in C# and XAML
问题描述
我一直在寻找,以小时,东西很简单:绑定一个WPF数据网格到DataTable中才能看到列在设计时。我不能得到任何的例子来为我工作。
I have been searching to hours for something very simple: bind a WPF datagrid to a datatable in order to see the columns at design-time. I can’t get any of the examples to work for me.
下面是C#code来填充里面的数据信息的数据表InfoWork:
Here is the C# code to populate the datatable InfoWork inside the dataset info:
info = new Info();
InfoTableAdapters.InfoWorkTableAdapter adapter = new InfoTableAdapters.InfoWorkTableAdapter();
adapter.Fill(info.InfoWork);
问题是无论我怎么申报信息或infoWork的Visual Studio / XAML找不到它。我曾尝试:
The problem is no matter how I declare ‘info’ or ‘infoWork’ Visual Studio/XAML can’t find it. I have tried:
<Window.Resources>
<ObjectDataProvider x:Key="infoWork" ObjectType="{x:Type local:info}" />
</Window.Resources>
我也试图从WPF codePLEX这个例子,但XAML甚至不喜欢的地方!关键字
I have also tried this example from wpf.codeplex, but XAML doesn’t even like the "local:" keyword!
<Window.Resources>
<local:info x:Key="infoWork"/>
</Window.Resources>
实际上有两个主要的问题在这里:
1)我如何申报在C#中的表InfoWork使XAML可以看到它?我想声明它在XAML没有成功存在于窗口类公共。
2)我如何申报XAML Windows资源,比较特别的数据集内的数据表?
There are really two main questions here: 1) How do I declare the table InfoWork in C# so that XAML can see it? I tried declaring it Public in the window class that XAML exists in with no success. 2) How do I declare the windows resource in XAML, specifcally the datatable inside the dataset?
出于好奇,是有原因的ItemsSource只是不显示为,在性能设计窗口中设置的属性?
Out of curiosity, is there a reason that ItemsSource just doesn't show up as a property that be set in the properties design window?
推荐答案
在下列方式使用Microsoft DataGrid中为我的作品:
Using the Microsoft DataGrid in the following way works for me:
在XAML我包括DataGrid的:
In XAML I include the DataGrid:
<WpfToolkit:DataGrid
Grid.Row="4" Grid.Column="0"
ItemsSource="{Binding Path=GridData, Mode=OneWay}" >
</WpfToolkit:DataGrid>
在我看来,我的模型揭露数据视图:
In my view model I expose a DataView:
public DataView GridData
{
get
{
DataSet ds = new DataSet("MyDataSet");
using (SqlConnection conn = new SqlConnection(ConnectionString))
{
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM HumanResources.Employee";
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
}
return ds.Tables[0].DefaultView;
}
}
这篇关于如何在数据表绑定到C#和XAML一个WPF数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!