我刚刚制定了一个将属性(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 回答
您的类应包含 Properties ,而不是 Variables .
DataGrid
只会查找公共属性而不是公共变量 .见here和here