What am I missing here? RRS feed

  • Question

  • Hi there,

    I'm trying to create and databind a very simple DataGrid in code behind, but something still goes wrong, as the DataGrid doesn't show up on the page (except for its border). The XAML page consists of a typical new standard page with a default LayoutRoot grid.

    When I run and stop with a breakpoint, I noticed that the collection gets filled, so there's some other point that I'm missing. Here's the code:

    Imports System.Windows.Controls
    Imports System.Collections.ObjectModel
    Imports System.ComponentModel
    Partial Public Class MainPage
      Inherits UserControl
      Implements INotifyPropertyChanged
      Dim dg As New DataGrid
      Private _l As New ObservableCollection(Of c)
      Public Property l As ObservableCollection(Of c)
          Return _l
        End Get
        Set(value As ObservableCollection(Of c))
          If value Is _l Then
            _l = value
          End If
        End Set
      End Property
      Public Sub New()
        dg.Width = 500
        dg.Height = 500
        dg.HorizontalAlignment = Windows.HorizontalAlignment.Center
        dg.VerticalAlignment = Windows.VerticalAlignment.Center
        For i As Integer = 0 To 25
          l.Add(New c With {.number = i, .square = i * i})
        dg.HeadersVisibility = DataGridHeadersVisibility.All
        dg.DataContext = l
        dg.ItemsSource = l
        dg.AutoGenerateColumns = True
      End Sub
      Public Event PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
      Public Sub NotifyPropertyChanged(info As String)
        If info = "list" Then RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
      End Sub
    End Class
    Public Class c
      Public number As Integer
      Public square As Integer
    End Class

    Thanks for your help!

    Monday, June 18, 2012 3:35 AM


  • Hi Rob K.

    In Class c, member and square lack of get and set method. So these two property value cannot be get in DataGrid.

    Please change Class c as below

    Public Class c
        Private mNumber As Integer
        Public Property number() As Integer
                Return mNumber
            End Get
            Set(ByVal value As Integer)
                mNumber = value
            End Set
        End Property
        Private mSquare As Integer
        Public Property square() As Integer
                Return mSquare
            End Get
            Set(ByVal value As Integer)
                mSquare = value
            End Set
        End Property
        'Public number As Integer
        'Public square As Integer
    End Class

    Best Regards,

    Tuesday, June 19, 2012 10:30 PM