[英]Binding a DataGrid
I've created a code-first C# project with Entity Framework and WPF. 我已经使用Entity Framework和WPF创建了代码优先的C#项目。 I have created an Entity named Personel Entity. 我创建了一个名为Personel Entity的实体。 I'm dragging and dropping that Entity to MainWindow but it doesn't show any data. 我将那个实体拖放到MainWindow,但是它不显示任何数据。 I think I have to do something in MainWindow.xaml.cs file but I don't know what to do. 我想我必须在MainWindow.xaml.cs文件中做一些事情,但我不知道该怎么办。 Here is the DataGrid code in xaml: 这是xaml中的DataGrid代码:
<DataGrid x:Name="personelEntityDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="19,259,18,10" RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridTextColumn x:Name="addressColumn" Binding="{Binding Address}" Header="Address" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="ageColumn" Binding="{Binding Age}" Header="Age" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
<DataGridTextColumn x:Name="phoneNumberColumn" Binding="{Binding PhoneNumber}" Header="Phone Number" Width="SizeToHeader"/>
</DataGrid.Columns>
</DataGrid>
Here is the code in MainWindow.xaml.cs file : 这是MainWindow.xaml.cs文件中的代码:
public partial class MainWindow : Window
{
private PersonelContext _context = new PersonelContext();
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource personelEntityViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personelEntityViewSource")));
}
}
Here is the code in PersonelContext.cs file : 这是PersonelContext.cs文件中的代码:
namespace Personel
{
public class PersonelContext : DbContext
{
public DbSet<PersonelEntity> Personels { get; set; }
}
}
There is nothing else about datagrid in code. 代码中没有其他关于datagrid的内容。 I know I need to add something but I don't know what to add. 我知道我需要添加一些东西,但是我不知道要添加什么。 Can you tell me what to do? 你能告诉我该怎么办吗?
Basically every binding is built on a DataContext
of particular FrameworkElement . 基本上,每个绑定都基于特定FrameworkElement的DataContext
构建。 In your case it is DataGrid
. 您的情况是DataGrid
。 Data is not updated because you've not initialized data context for UseControl
. 数据未更新,因为您尚未初始化UseControl
数据上下文。
Please do not confuse it with Entity Frameworks DbContext
which has nothing to do with controls DataContext
. 请不要将其与与控件DataContext
无关的Entity Frameworks DbContext
混淆。
So to make your screen working just add following line into Window_Loaded
method: 因此,要使屏幕正常工作,只需Window_Loaded
添加到Window_Loaded
方法中:
this.DataContext = _context.Personels.ToList();
Make one Change in Xaml as below 在Xaml中进行如下更改
ItemSource= {Binding}
to
ItemSource= {Binding Path=.}
and in Code behind 并在后面的代码中
personelEntityDataGrid.ItemSource =_context.Personels.ToList();
(if above not works try OR part). (如果以上方法无效,请尝试“或”部分)。
or 要么
personelEntityDataGrid.DataContext =_context.Personels.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.