【发布时间】:2023-04-08 16:49:01
【问题描述】:
这里我的 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; }
}
所以我的问题是如何将数据表中的数据绑定到数据网格。我必须改变的地方请建议我。
编辑: 我想我必须在“StudentDetails”中添加一些内容
编辑 2 如前所述,我添加了
public List<StudentMarkDetails> StudentMarks { get; set; }
在StudentDetails 中但得到空引用异常。虽然
public List<StudentMarkDetails> StudentMarks = new List<StudentMarkDetails>();
添加的数据未显示在 DataGrid 中。
【问题讨论】:
-
你在 StackOverflow 上查看过其他类似的问题吗?看来他们中的几个应该能够将您推向正确的方向stackoverflow.com/questions/20770438/…stackoverflow.com/questions/30445620/…
-
@MilanNankov 是的,我已经搜索了所有这些问题,这里我的问题是我要添加数据的 DataGrid 存在于 UserControl 中。我不能直接用它的名字访问datagrid。
-
创建一个类似
StudentMarkDetails的类,将类的集合添加到StudentDetails,从ShowDetails方法填充该集合,并将绑定更改为指向它。例如,如果您将public List<StudentMarkDetails> StudentMarks { get; set; }添加到StudentDetails类,那么您的 dg_Marks 绑定将是ItemsSource="{Binding StudentDetails}" -
@Rachel 更改后也没有效果。我想我必须为 dg_Marks 使用 DataContext