【问题标题】:Trying to bind a list of objects to a WPF datagrid control and seeing blank lines尝试将对象列表绑定到 WPF 数据网格控件并看到空行
【发布时间】:2014-11-11 03:23:57
【问题描述】:

我被困住了,需要一些帮助。我花了一整天的时间试图解决这个问题。我已经阅读了很多应该如何做到这一点的例子,看起来我做得正确,但显然我错过了一些东西。我有一个带有一个按钮和一个数据网格的空白表单。当我单击按钮时,我想将点列表加载到 datagrid 控件中。必须加载数据,因为我在网格中看到三个空白行(没有标题),但没有数据。请帮忙!谢谢!

Class MainWindow

Class Point
    Public Inc As String
    Public AZ As String
    Public MD As String
    Public TD As String
End Class


Private Sub Button_Click(sender As Object, e As RoutedEventArgs)

    Dim mySurvey As New List(Of Point)
    Dim myPoint1 As New Point
    Dim myPoint2 As New Point

    myPoint1.AZ = "1"
    myPoint1.Inc = "2"
    myPoint1.MD = "100"
    myPoint1.TD = "98"

    myPoint2.AZ = "10"
    myPoint2.Inc = "20"
    myPoint2.MD = "1000"
    Point2.TD = "980"


    mySurvey.Add(myPoint1)
    mySurvey.Add(myPoint2)
    dgSurvey.ItemsSource = mySurvey

End Sub

结束类

XAML

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Button" HorizontalAlignment="Left" Height="28" Margin="45,30,0,0" VerticalAlignment="Top" Width="57" Click="Button_Click"/>
    <DataGrid x:Name="dgSurvey" HorizontalAlignment="Left" Margin="45,78,0,0" VerticalAlignment="Top" Height="172" Width="275"/>

</Grid>

【问题讨论】:

  • 你能分享你的 xaml 吗?
  • 刚刚将其添加到问题中。谢谢尼特
  • 我认为你应该在 Point 类中定义属性,而不是公共字段,这样才能正常工作。
  • icebat 你太棒了。非常感谢。问题解决了。发布为答案,我会选择它。

标签: wpf vb.net datagrid wpfdatagrid


【解决方案1】:

要使绑定生效,您应该在 Point 类中定义 properties,而不是公共字段。 这在MSDN: Binding Sources Overview中有明确说明:

您用作绑定的绑定源属性的属性必须是公共的 你的类的属性。显式定义的接口属性不能 出于绑定目的而访问,受保护的、私有的、内部的、 或没有基本实现的虚拟属性。

您不能绑定到公共字段。

【讨论】:

    猜你喜欢
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 2017-05-14
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    相关资源
    最近更新 更多