Wpf-Datagrid和ObservableCollection-AutoGenerateColumns?

时间:2015-05-26 05:23:47

标签: c# wpf datagrid


我刚刚制定了一个解决方案,将属性(DP)绑定到Datagrid。没关系,一切都很顺畅和柔软。但是当我运行应用程序时,DataGrid只显示空白行!(假设有10条记录,我看到10个没有列的空行!)。该属性是一个ObservableCollection(客户)。
这是我的Xaml:

DataGrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" x:Name="DGCustomers" AutoGenerateColumns="True"  ItemsSource="{Binding CustomersOC}"/>

我要求你一瞥我的代码后面:

 Public Class CustomersEditor 
 Dim CustomersCollection As New ObservableCollection(Of Customer)
      Public Class Customer
        Public CustomerID As String
        Public CustomerName As String
        Public ContactPerson As String
        Public Address As String
        Public Area As String
        Public City As String
        Public Pincode As String
        Public ContactNumber As String
        Public OnePercentDiscount As Boolean
        Public Sub New(ByVal Cid As String, ByVal Cname As String, ByVal Cperson As String, ByVal Addr As String, ByVal Area1 As String, ByVal Cty As String, ByVal PCode As String, ByVal CNo As String, ByVal OPD As Boolean)
            CustomerID = Cid
            CustomerName = Cname
            ContactPerson = Cperson
            Address = Addr
            Area = Area1
            City = Cty
            Pincode = PCode
            ContactNumber = CNo
            OnePercentDiscount = OPD

        End Sub

    End Class
 Public Shared CustomersColl As DependencyProperty = DependencyProperty.Register("CustomersOC", GetType(ObservableCollection(Of Customer)), GetType(CustomersEditor))
    Public Property CustomersOC As ObservableCollection(Of Customer)
        Get
            Return GetValue(CustomersColl)

        End Get
        Set(ByVal value As ObservableCollection(Of Customer))
            SetValue(CustomersColl, value)

        End Set
    End Property
      Public Sub New()
        InitializeComponent()
      'I have added records to CustomersCollection with Access Database Reader.such as CustomersCollection.Add(new Customer(....))
       SetValue(CustomersColl, CustomersCollection)
       DGCustomers.DataContext = Me


    End Sub
End Class 

是否有任何解决方案,我可以使用九列和适当的数据查看DataGrid?[AutoGenerateColumns失败!但我从数据库中获取数据的代码经过精炼,完善和有效。
对于C#用户,我有converter.telerik.com

1 个答案:

答案 0 :(得分:3)

您的课程应包含属性,而不是变量DataGrid只会查找公共属性而不是公共变量。

请参阅herehere