我有一个 DataGrid,定义如下:
<DataGrid Name="dgResults"
IsReadOnly="True"
AutoGenerateColumns="True"
AllowDrop="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserResizeRows="False"
CanUserSortColumns="False"
ColumnWidth="120"
Margin="15,10,10,10"
Visibility="Collapsed"
ItemsSource="{Binding}"/>
出于某种原因,绑定到它时没有显示任何数据。正在显示正确数量的行,但它们都是空的。这是我的代码:
dgResults.DataContext = dtTopTwoHundredResults.AsDataView();
dgResults.AutoGeneratingColumn += new EventHandler<DataGridAutoGeneratingColumnEventArgs>(dataGrid_AutoGeneratingColumn);
dgResults.Visibility = Visibility.Visible;
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
//Sets the DataGrid's headers to be TextBlocks, which solves a problem whereby underscore characters in the header are ignored.
TextBlock block = new TextBlock();
block.Text = e.Column.Header.ToString();
e.Column.Header = block;
}
这绝对不是数据源的问题,因为数据应该包含在其中。它只是DataGrid。这是它的显示方式,即正确的行数但没有数据:
我使用 Snoop 来确定文本是否实际包含在 DataGrid 中,我得到了这个:
System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
System.Windows.Data Error: 40 : BindingExpression path error: 'Employee identification number' property not found on 'object' ''DataRowView' (HashCode=51298929)'. BindingExpression:Path=Employee identification number. Foreign key to Employee.BusinessEntityID.; DataItem='DataRowView' (HashCode=51298929); target element is 'PropertyInformation' (HashCode=11239682); target property is 'Value' (type 'Object')