将数据表绑定到WPF datagrid

时间:2016-12-30 15:11:58

标签: wpf vb.net xaml binding datagrid

我的XAML如下:

<DataGrid x:Name="WaterfallDataGrid" HorizontalAlignment="Left" Height="540" Margin="10,410,0,0" VerticalAlignment="Top" Width="1650" CanUserSortColumns="False" ColumnWidth="60">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Load" Binding="{Binding Load}"></DataGridTextColumn>
        <DataGridTextColumn Header="PF" Binding="{Binding PF}"></DataGridTextColumn>
        <DataGridTextColumn Header="Spare" Binding="{Binding Spare}"></DataGridTextColumn>
    </DataGrid.Columns>
    </DataGrid>

我的VB.net代码如下:

 Dim dt3 As New DataTable("Waterfall")
        dt3.Columns.Add("Load")
        dt3.Columns.Add("PF")
        dt3.Columns.Add("Spare")
dt3.rows.add(New Objecet() {"full load", "0.8", "20%"})
WaterfallDataGrid.itemSource = dt3.defaultview

当数据表确实显示在数据网格上时,除了我在XAML中创建的原始3列之外,它还会创建另外3个新列(Load,PF和Spar)。

如何将VB.net代码绑定到XAML数据网格?

enter image description here

以下是Ed的代码

之后的屏幕截图

以下是我在Mark的评论后修改的代码。

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Datagrid_Binding"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="WaterfallDataGrid" HorizontalAlignment="Left" Height="170" Margin="85,65,0,0" VerticalAlignment="Top" Width="340" AutoGenerateColumns="False"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Height="25" Margin="380,255,0,0" VerticalAlignment="Top" Width="45"/>

    </Grid>
</Window>

这是VB代码

Class MainWindow
    Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
        Dim dt3 As New DataTable("Waterfall")
        dt3.Columns.Add("Load")
        dt3.Columns.Add("PF")
        dt3.Columns.Add("Spare")
        'dt3.rows.add(New Object() {"full load", "0.8", "20%"})
        dt3.Rows.Add("full load", "0.8", "20%")
        WaterfallDataGrid.ItemsSource = dt3.DefaultView
    End Sub
End Class

数据网格根本不显示任何数据。它以某种方式显示折叠的行。屏幕截图如下:

enter image description here

我认为问题是,如果我没有在XAML中预定义列并且我在vb.net中定义了行但是autogeneratecolumn设置为false,则VB无法以编程方式创建列在运行时期间,我在这里看到的是折叠的行。

我认为方法是预先定义XAML中的列,然后将数据绑定到XAML列,但没有任何代码可用....真的很令人沮丧。

2 个答案:

答案 0 :(得分:2)

我认为问题中的屏幕截图可能与显示的代码无关。如果初始代码的唯一问题是生成了额外的列,则只需将AutoGenerateColumns="False"添加到DataGrid XAML:

<DataGrid x:Name="WaterfallDataGrid"
          HorizontalAlignment="Left"
          Height="540"
          Margin="10,410,0,0"
          VerticalAlignment="Top"
          Width="1650"
          CanUserSortColumns="False"
          ColumnWidth="60"
          AutoGenerateColumns="False">

此外,由于DataRowCollection.Add采用ParamArray参数,因此您无需创建Object数组,只需单独传递列值,这样更容易阅读,例如

dt3.Rows.Add("full load", "0.8", "20%")

答案 1 :(得分:1)

当您将DataGrid的AutoGenerateColumns属性设置为False时,您需要自己明确定义列,因此使用原始XAML标记并添加AutoGenerateColumns =“False”,您应该看到三列:

<DataGrid x:Name="WaterfallDataGrid" HorizontalAlignment="Left" Height="540" Margin="10,410,0,0" VerticalAlignment="Top" Width="1650" CanUserSortColumns="False" ColumnWidth="60" 
AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Load" Binding="{Binding Load}"></DataGridTextColumn>
        <DataGridTextColumn Header="PF" Binding="{Binding PF}"></DataGridTextColumn>
        <DataGridTextColumn Header="Spare" Binding="{Binding Spare}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

要删除用户可以添加新行的额外空白行,可以将CanUserAddRows属性设置为False:

<DataGrid x:Name="WaterfallDataGrid" CanUserAddRows="False" ...
  

是的,我知道这一点。问题是,虽然我可以在XAML中创建3列,但我无法通过上面的VB代码将数据绑定到列。

以下完整的代码示例对我有效。

<强> MainWindow.xaml.vb:

Class MainWindow
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim dt3 As New DataTable("Waterfall")
        dt3.Columns.Add("Load")
        dt3.Columns.Add("PF")
        dt3.Columns.Add("Spare")
        'dt3.rows.add(New Object() {"full load", "0.8", "20%"})
        dt3.Rows.Add("full load", "0.8", "20%")
        WaterfallDataGrid.ItemsSource = dt3.DefaultView

    End Sub
End Class

<强> MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplicationVb1"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2" x:Class="MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid x:Name="WaterfallDataGrid" CanUserSortColumns="False" ColumnWidth="60" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Load" Binding="{Binding Load}"></DataGridTextColumn>
                <DataGridTextColumn Header="PF" Binding="{Binding PF}"></DataGridTextColumn>
                <DataGridTextColumn Header="Spare" Binding="{Binding Spare}"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>