如何将数据绑定到WPF中UserControl中的DataGrid

时间:2016-11-04 06:17:42

标签: c# wpf datagrid

这里我的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; }
}

所以我的问题是如何将datatable中的数据绑定到dataGrid。我需要改变的地方请建议我。

修改   我想我必须在“StudentDetails”中添加一些东西

编辑2 如上所述,我添加了

public List<StudentMarkDetails> StudentMarks { get; set; }
<{1>}中的

但是获得空引用异常。即使

StudentDetails

添加的数据未在DataGrid中显示。

1 个答案:

答案 0 :(得分:0)

通过设置UserControls的DataContext,我认为您处于正确的轨道上。问题是您正在将DataContext设置为&#34; request&#34;这限制了UserControl中的控件可以绑定到的内容。

DataGrid控件需要一个要绑定的内容列表。当您使用&#34; student1.DataContext = request &#34; + ItemsSource =&#34; {Binding}&#34;在您的网格上,您实际上是将网格绑定到单个实体。

您应该做的是将网格绑定到DataTable本身。尝试像这样设置DataContext - student1.DataContext = dataTable.DefaultView ;

如果需要同时绑定标签和DataGrid,则必须创建一个ViewModel,它将UserControl所需的所有数据组合在一起并将其用作DataContext。 ViewModel对WPF至关重要。如果你还没有研究过它们,我建议你这样做。这是体面的介绍 - Model-View-ViewModel (MVVM) Explained