I've got a DataGrid, as defined below:
我有一个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}"/>
For some reason, no data is showing when binding to it. The correct amount of rows are displaying, however they are all empty. This is my code:
出于某种原因,绑定到它时没有数据显示。显示正确的行数,但它们都是空的。这是我的代码:
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;
}
It's definitely not a problem with the data source, as the data is contained inside it as it should be. It's just the DataGrid. This is how it's displaying, i.e. the correct amount of rows but no data:
这绝对不是数据源的问题,因为数据应该包含在其中。它只是DataGrid。这是它的显示方式,即正确的行数但没有数据:
I used Snoop to find out if the text is actually contained in the DataGrid, and I'm getting this:
我使用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')
3 个解决方案
#1
4
The problem over here is the fact that punctuation is involved in the column headers. Removing the punctuation solved the issue. Hopefully someone with the same problem will read this, as it took me many hours to realise.
这里的问题是列标题中涉及标点符号。删除标点符号解决了这个问题。希望有同样问题的人会读到这个,因为我花了很多时间才意识到这一点。
#2
0
From your code, it looks like you just want custom column headers. If so, you'll probably want to define your columns explicitly in the XAML:
从您的代码中,您似乎只想要自定义列标题。如果是这样,您可能希望在XAML中明确定义列:
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>
The way you have it now, it seems that the XAML parser (or whatever) is looking for property names that match what's in the column header (or something like that), and it isn't finding properties on your ItemsSource objects that match the column header.
你现在的方式,似乎XAML解析器(或其他)正在寻找与列标题中的内容匹配的属性名称(或类似的东西),并且它没有在ItemsSource对象上找到与之匹配的属性列标题。
#3
0
You could explicitly set your Data Grid Columns as so, problem at that point your data grid is limited in what it can display cause headers are explicitly set. The problem is when you are setting the headers in your autogeneratedColumn event handler the text block is ignoring the underscore like you said but that point wpf doesnt know how to bind column data to that header. That's why you are getting error with the binding path. The "Employee identification number" is what its trying to bind back into your dataview, but in your dataview its "Employee_identification_number". The solution below solves your problem but limits you to only displaying that particular dataview. If thats not an issue then this is your solution.
您可以显式设置数据网格列,此时出现问题,您的数据网格受限于显示原因标头的显式设置。问题是当你在autogeneratedColumn事件处理程序中设置标题时,文本块忽略了你所说的下划线,但是这一点wpf不知道如何将列数据绑定到该标题。这就是你遇到绑定路径错误的原因。 “员工识别号码”是它试图绑定到您的数据视图的内容,但在您的数据视图中是“Employee_identification_number”。下面的解决方案可以解决您的问题,但限制您只显示特定的数据视图。如果那不是问题那么这就是你的解决方案。
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>
#1
4
The problem over here is the fact that punctuation is involved in the column headers. Removing the punctuation solved the issue. Hopefully someone with the same problem will read this, as it took me many hours to realise.
这里的问题是列标题中涉及标点符号。删除标点符号解决了这个问题。希望有同样问题的人会读到这个,因为我花了很多时间才意识到这一点。
#2
0
From your code, it looks like you just want custom column headers. If so, you'll probably want to define your columns explicitly in the XAML:
从您的代码中,您似乎只想要自定义列标题。如果是这样,您可能希望在XAML中明确定义列:
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>
The way you have it now, it seems that the XAML parser (or whatever) is looking for property names that match what's in the column header (or something like that), and it isn't finding properties on your ItemsSource objects that match the column header.
你现在的方式,似乎XAML解析器(或其他)正在寻找与列标题中的内容匹配的属性名称(或类似的东西),并且它没有在ItemsSource对象上找到与之匹配的属性列标题。
#3
0
You could explicitly set your Data Grid Columns as so, problem at that point your data grid is limited in what it can display cause headers are explicitly set. The problem is when you are setting the headers in your autogeneratedColumn event handler the text block is ignoring the underscore like you said but that point wpf doesnt know how to bind column data to that header. That's why you are getting error with the binding path. The "Employee identification number" is what its trying to bind back into your dataview, but in your dataview its "Employee_identification_number". The solution below solves your problem but limits you to only displaying that particular dataview. If thats not an issue then this is your solution.
您可以显式设置数据网格列,此时出现问题,您的数据网格受限于显示原因标头的显式设置。问题是当你在autogeneratedColumn事件处理程序中设置标题时,文本块忽略了你所说的下划线,但是这一点wpf不知道如何将列数据绑定到该标题。这就是你遇到绑定路径错误的原因。 “员工识别号码”是它试图绑定到您的数据视图的内容,但在您的数据视图中是“Employee_identification_number”。下面的解决方案可以解决您的问题,但限制您只显示特定的数据视图。如果那不是问题那么这就是你的解决方案。
<DataGrid.Columns>
<DataGridTextColumn Header="Employee identification number"
Binding="{Binding Path=EmployeeID}"
</DataGridTextColumn>
<Insert other column definitions here/>
</DataGrid.Columns>