[英]How to bind data to the DataGrid present in the UserControl in WPF
在這里,我的UserControl為:
<Border Grid.Column="0" BorderBrush="Black" BorderThickness="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="5*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Student Name"/>
<TextBlock Text="{Binding NAME}" Grid.Column="1" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Row="1" Grid.Column="0" Content="Roll No"/>
<TextBlock Text="{Binding ROLL_NO}" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<Label Grid.Row="2" Grid.Column="0" Content="Class Teacher Name"/>
<TextBlock Text="{Binding CLASS_TEACHER}" Grid.Column="1" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<DataGrid Grid.Row="3" Grid.ColumnSpan="2" x:Name="dg_Marks" IsReadOnly="False" ItemsSource="{Binding}" ColumnWidth="*" RowHeaderWidth="0" VerticalAlignment="Top" CanUserAddRows="False" Height="78" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Binding="{Binding TEST_NO}" Header="Test No" />
<DataGridTextColumn IsReadOnly="True" Binding="{Binding ENGLISH}" Header="English" />
<DataGridTextColumn IsReadOnly="True" Binding="{Binding SOCIAL}" Header="Social" />
<DataGridTextColumn IsReadOnly="True" Binding="{Binding SCIENCE}" Header="Science" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Border>
我在MainWindow中的不同位置使用了此控件。 這是我的MainWindow.xaml
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>
<Label Content="Student1:" Grid.Column="0" />
<local:StudentUserControl x:Name="student1" Margin="10,25,5,0" Width="250" Height="200" VerticalAlignment="Top" />
<Label Content="Student2:" Grid.Column="1" />
<local:StudentUserControl Grid.Column="1" x:Name="student2" Margin="10,25,5,0" Width="250" Height="200" VerticalAlignment="Top" />
<Label Content="Student3:" Grid.Column="2" />
<local:StudentUserControl Grid.Column="2" x:Name="student3" Margin="10,25,5,0" Width="250" Height="200" VerticalAlignment="Top" />
</Grid>
我可以將數據庫中的數據綁定到標簽。 但是我無法將數據綁定到DataGrid。 這是我的MainWindow.cs
private void ShowDetails(string strName)
{
StudentDetails request = new StudentDetails();
String retrieveCommand = "Select * from STUDENT_DETAILS where NAME='" + strName + "'";
DataTable dataTable = MDBConnection.RetreiveTableData(retrieveCommand);
request.NAME = dataTable.Rows[0]["NAME"].ToString();
request.ROLL_NO = dataTable.Rows[0]["ROLL_NO"].ToString();
request.CLASS_TEACHER = dataTable.Rows[0]["CLASS_TEACHER"].ToString();
retrieveCommand = "Select * from STUDENT_MARKS_DETAILS where NAME='" + strName + "'";
dataTable = MDBConnection.RetreiveTableData(retrieveCommand);
// How to bind data to the datagrid of the UserControl
student1.DataContext = request;
}
我的StudentDetails類看起來像
public class StudentDetails
{
public String NAME { get; set; }
public String ROLL_NO { get; set; }
public String CLASS_TEACHER { get; set; }
}
所以我的問題是如何將數據表中的數據綁定到dataGrid。 我必須更改的地方建議我。
編輯:我想我必須添加一些東西到“ StudentDetails”
編輯2如前所述,我添加了
public List<StudentMarkDetails> StudentMarks { get; set; }
在StudentDetails
但得到Null引用異常。 即使
public List<StudentMarkDetails> StudentMarks = new List<StudentMarkDetails>();
被添加的數據未顯示在DataGrid中。
我認為通過設置UserControls的DataContext可以使您走上正確的道路。 問題是您將DataContext設置為“ request”,這限制了UserControl中的控件可以綁定到的內容。
DataGrid控件需要綁定的對象列表。 當您在網格上使用“ student1.DataContext = request ” + ItemsSource =“ {Binding}”時 ,實際上是在將網格綁定到單個實體。
相反,您應該做的是將網格綁定到DataTable本身。 嘗試像這樣設置DataContext- student1.DataContext = dataTable.DefaultView ;
如果需要同時綁定標簽和DataGrid,則必須創建一個ViewModel,該ViewModel組合UserControl所需的所有數據並將其用作DataContext。 ViewModel對於WPF是必不可少的。 如果您還沒有研究過它們,我建議您去做。 這是不錯的介紹-Model-View-ViewModel(MVVM)解釋了 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.