gpt4 book ai didi

c# - Wpf-Datagrid 和 ObservableCollection-AutoGenerateColumns?

转载 作者:行者123 更新时间:2023-11-30 16:07:41 31 4
gpt4 key购买 nike


我刚刚制定了一个将属性 (DP) 绑定(bind)到 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 SubEnd Class 

是否有任何解决方案可以让我查看具有九列和适当数据的 DataGrid?[AutoGenerateColumns 失败!但是我从数据库中获取数据的代码是精炼的、完美的并且可以工作。]
对于 C# 用户,我有 converter.telerik.com

最佳答案

您的类应该包含属性,而不是变量DataGrid 将只查找公共(public)属性而不是公共(public)变量。

参见 here , 和 here

关于c# - Wpf-Datagrid 和 ObservableCollection-AutoGenerateColumns?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30450096/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com