问题
I'm having a problem and believe something simple, just can't get a grip on it. I have a View model for my data context of the window. On it I have a private "DataTable" that is populated from a database query. No problem. I have a public DataView property {get;set;} that returns the DataTable.DefaultView. I bind the DataGrid on the form to the path of the DataView property on the view model and run the form. No problem, comes up, columns displayed properly, click on the rows, no problem.
Now, the problem. Click on the header for what I thought auto-handles the data sorting and it comes up with an error that
'MyTable' type does not have property named '[MyColumn]', so cannot sort data collection.
The confusing part here. The binding is to the VIEW (MyTableView) originating from MyTable.DefaultView, and it DOES have a column "MyColumn" as it is properly displayed in the grid. What am I missing.
Just to clarify, here's some of the xaml and code from the view model
XAML
<DataGrid AutoGenerateColumns="False"
Name="dataMyData"
ItemsSource="{Binding Path=MyTableView, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"
SelectedItem="{Binding Path=MyCurrentViewRecord, Mode=TwoWay}" >
<DataGrid.Columns>
<DataGridTextColumn Header="#" Binding="{Binding Path=[MyRecord], NotifyOnSourceUpdated=True}" />
<DataGridTextColumn Header="MyColumn" Binding="{Binding Path=[MyColumn],NotifyOnSourceUpdated=True}"/>
</DataGrid.Columns>
</DataGrid>
C#
PRIVATE DataTable MyTable;
public DataView MyTableView
{
get { return MyTable.DefaultView; }
}
回答1:
Actually, found it...
I had to explicitly add
SortMemberPath="MyColumn" (for the column definition of the DataGrid)
It was choking on the [] brackets in the binding, but if I didn't include the brackets in the binding as it is a column source for display, it wouldn't show the data...
来源:https://stackoverflow.com/questions/8010614/datagrid-header-sort-error-bound-to-dataview