DataGrid 不显示数据而是显示模型 Class 名称
DataGrid is not show the data instead its show Model Class Name
当我将数据 collection 绑定到我的数据网格时,它不显示数据而是显示模型的数据 class 命名多少行我有那么多时间。我尝试了普通的 WPF 数据网格和 Dev Express 数据网格,但对我来说没有任何效果。
XAML:
使用 WPF 数据网格
<DataGrid ItemsSource="{Binding EmpDetails}" AutoGenerateColumns="True"/>
使用 Dev Express DataGrid
<dxg:GridControl ItemsSource="{Binding EmpDetails}" AutoPopulateColumns="True" AutoGenerateColumns="AddNew" >
<dxg:GridControl.View>
<dxg:TableView />
</dxg:GridControl.View>
</dxg:GridControl>
我的 collection 中有 2 条记录,请在下图中找到
https://i.stack.imgur.com/pdjQt.jpg
Dev Express DataGrid 运行 时间图像:
https://i.stack.imgur.com/Iq9VC.jpg
WPF DataGrid 运行 时间图像:
https://i.stack.imgur.com/HMgd8.png
感谢您花时间回答我的问题。
确保从 "EmpDetails" 源 属性 返回的 IEnumerable 中的类型 T 具有 public 属性 (不是字段)您希望在 DataGrid 中自动生成的每一列。请参考下面的示例代码。
型号:
Public Class EmployeeDetails
Public Property ColumnA As String
Public Property ColumnB As String
End Class
查看模型:
Public Class ViewModel
Public Sub New()
EmpDetails = New List(Of EmployeeDetails)
EmpDetails.Add(New EmployeeDetails With {.ColumnA = "A1", .ColumnB = "B1"})
EmpDetails.Add(New EmployeeDetails With {.ColumnA = "A2", .ColumnB = "B2"})
End Sub
Public Property EmpDetails As ObservableCollection(Of EmployeeDetails)
End Class
查看:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding EmpDetails}" AutoGenerateColumns="True"/>
</Grid>
MainWindow
将 EmployeeDetails 设为 Observable 集合,table 的所有列写为 属性 with On属性Change 包括模型 Class.
EX:
private string firstName;
public string FirstName
{
get {return firstName;}
set { firstName = value; OnPropertyChange("FirstName");}
}
当我将数据 collection 绑定到我的数据网格时,它不显示数据而是显示模型的数据 class 命名多少行我有那么多时间。我尝试了普通的 WPF 数据网格和 Dev Express 数据网格,但对我来说没有任何效果。
XAML:
使用 WPF 数据网格
<DataGrid ItemsSource="{Binding EmpDetails}" AutoGenerateColumns="True"/>
使用 Dev Express DataGrid
<dxg:GridControl ItemsSource="{Binding EmpDetails}" AutoPopulateColumns="True" AutoGenerateColumns="AddNew" >
<dxg:GridControl.View>
<dxg:TableView />
</dxg:GridControl.View>
</dxg:GridControl>
我的 collection 中有 2 条记录,请在下图中找到
https://i.stack.imgur.com/pdjQt.jpg
Dev Express DataGrid 运行 时间图像:
https://i.stack.imgur.com/Iq9VC.jpg
WPF DataGrid 运行 时间图像:
https://i.stack.imgur.com/HMgd8.png
感谢您花时间回答我的问题。
确保从 "EmpDetails" 源 属性 返回的 IEnumerable 中的类型 T 具有 public 属性 (不是字段)您希望在 DataGrid 中自动生成的每一列。请参考下面的示例代码。
型号:
Public Class EmployeeDetails
Public Property ColumnA As String
Public Property ColumnB As String
End Class
查看模型:
Public Class ViewModel
Public Sub New()
EmpDetails = New List(Of EmployeeDetails)
EmpDetails.Add(New EmployeeDetails With {.ColumnA = "A1", .ColumnB = "B1"})
EmpDetails.Add(New EmployeeDetails With {.ColumnA = "A2", .ColumnB = "B2"})
End Sub
Public Property EmpDetails As ObservableCollection(Of EmployeeDetails)
End Class
查看:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel />
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding EmpDetails}" AutoGenerateColumns="True"/>
</Grid>
MainWindow
将 EmployeeDetails 设为 Observable 集合,table 的所有列写为 属性 with On属性Change 包括模型 Class.
EX:
private string firstName;
public string FirstName
{
get {return firstName;}
set { firstName = value; OnPropertyChange("FirstName");}
}