使用实体框架将数据绑定到 WPF 中的数据网格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31339762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding data to a data-grid in WPF with Entity Framework
提问by Gisiota
I've looked all over the web for this, and nothing I found seems to help.
我在网上到处找这个,我发现没有任何帮助。
I made a model and added the model to a data source as an object. I assumed it would work like a data set where I can just drag and drop onto a form and it would bind the data for me. But it keeps showing blank when I drag and drop from the model. so I looked online and saw that some code-behind was required and this is what I have and its still blank. Any ideas what Im doing wrong?
我制作了一个模型并将该模型作为对象添加到数据源中。我认为它会像一个数据集一样工作,我可以将它拖放到一个表单上,它会为我绑定数据。但是当我从模型中拖放时它一直显示为空白。所以我在网上看了一下,发现需要一些代码隐藏,这就是我所拥有的,它仍然是空白的。任何想法我做错了什么?
public partial class form1: Window
{
ComEntities context;;
public form1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
context = new ComEntities();
System.Windows.Data.CollectionViewSource comEntitiesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("comEntitiesViewSource")));
var permits = (from c in context.tBLPER.Local select c);
this.DataContext = context.tBLPER.Local;
tBLPERDataGrid.ItemsSource = context.tBLPER.Local;
}
}
XAML:
XAML:
<DataGrid x:Name="tBLPERDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10,10,10,413" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
</DataGrid>
采纳答案by octavioccl
You need to materialize your query (bring the data to memory). You can do that calling the ToList()method, but even better is do this:
您需要具体化您的查询(将数据带入内存)。您可以调用该ToList()方法,但更好的是这样做:
context.TBLPER.Load();
this.DataContext = context.TBLPER.Local; // set the Window DataContext property
Localproperty gets an ObservableCollection<T>that represents a local view of all Added, Unchanged, and Modifiedentities in this set. This local view will stay in sync as entities are added or removed from the context. Likewise, entities added to or removed from the local view will automatically be added to or removed from the context.
Local属性获取一个ObservableCollection<T>代表所有的本地视图Added,Unchanged和Modified在此设实体。在上下文中添加或删除实体时,此本地视图将保持同步。同样,添加到本地视图或从本地视图中删除的实体将自动添加到上下文中或从上下文中删除。
In case you need to filter your data before (suppose your entity has a property named Ageand want the users older than 20), then you can do this:
如果您之前需要过滤数据(假设您的实体有一个名为的属性Age并希望用户年龄超过 20 岁),那么您可以执行以下操作:
context.TBLPER.Where(t=>t.Age>20).Load();
this.DataContext = context.TBLPER.Local;
Another thing, if you want to set the ItemSourceproperty of your Grid in the code behind of your window, it don't make sense create a binding to that property in your xaml code, so remove it:
另一件事,如果您想ItemSource在窗口后面的代码中设置Grid的属性,则在 xaml 代码中创建对该属性的绑定是没有意义的,因此将其删除:
<DataGrid ... ItemsSource="{Binding}" ...>
If you are going to do this:
如果你打算这样做:
tBLPERDataGrid.ItemsSource=context.TBLPER.Local;
回答by 3-14159265358979323846264
You shouldn't be setting ItemsSourcetwice (just set it in your code behind - remove ItemsSource="{Binding}").
您不应该设置ItemsSource两次(只需在后面的代码中设置它 - remove ItemsSource="{Binding}")。
Also, you should set AutoGenerateColumns="True"because without that you need to add DataGridXColumnelements to the DataGrid.
此外,您应该设置,AutoGenerateColumns="True"因为如果没有,您需要将DataGridXColumn元素添加到DataGrid.
Have a look here for more details ... http://www.wpf-tutorial.com/datagrid-control/custom-columns/
在这里查看更多详细信息... http://www.wpf-tutorial.com/datagrid-control/custom-columns/
You may also want to put a breakpoint on the tBLPERDataGrid.ItemsSource = permits;line so you can inspect permitsto confirm it contains the data you expect.
您可能还想在该tBLPERDataGrid.ItemsSource = permits;行上放置一个断点,以便您可以检查permits以确认它包含您期望的数据。
回答by ntohl
If You want to use Model (also ItemsSource="{Binding}"is a hint for me, that You want), than don't hard code the ItemsSource. You create the Model's object in a CEntities context;variable, but You should set it to the DataContextproperty of the Window like this:
如果您想使用模型(ItemsSource="{Binding}"对我来说也是一个提示,您想要),不要对 ItemsSource 进行硬编码。您在CEntities context;变量中创建模型的对象,但您应该将其设置DataContext为 Window的属性,如下所示:
DataContext = new CEntities();
and remove the line
并删除该行
tBLPERDataGrid.ItemsSource = permits;
Now the ItemsSource is coming from the CEntitiesinstance.
现在 ItemsSource 来自CEntities实例。
回答by Murilo
If you set AutoGenerateColumns="False" you should provide the columns definition on XAML or set AutoGenerateColumns="True". This is the first thing to do.
如果设置 AutoGenerateColumns="False",则应在 XAML 上提供列定义或设置 AutoGenerateColumns="True"。这是要做的第一件事。

