我已阅读有关此主题的所有帖子,但似乎没有解决我的情况。我试图将一些简单的数据加载到数据网格中,然后允许用户编辑数据。我遇到的问题是网格不允许编辑单元格。我使用的是wpf和VB。这是创建网格的XAML:
<DataGrid
x:Name="dgrMain"
CanUserAddRows="True"
CanUserDeleteRows="True"
AutoGenerateColumns="False"
SelectionMode="Single"
SelectionUnit="CellOrRowHeader"
IsReadOnly="False"
ItemsSource="{Binding}">
<DataGrid.Columns>
<DataGridTextColumn
Header="Col1"
Binding="{Binding Path=Col1}"
IsReadOnly="False" />
<DataGridTextColumn
Header="Col2"
Binding="{Binding Path=Col2}"
IsReadOnly="False" />
<DataGridTextColumn
Header="Col3"
Binding="{Binding Path=Col3}"
IsReadOnly="False" />
</DataGrid.Columns>
</DataGrid
以下是加载数据的代码:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim dt As DataTable = New DataTable("OriginalData")
Dim col As DataColumn
col = New DataColumn
col.DataType = Type.GetType("System.Int32")
col.ColumnName = "Col1"
col.ReadOnly = False
dt.Columns.Add(col)
col = New DataColumn
col.DataType = Type.GetType("System.String")
col.ColumnName = "Col2"
col.ReadOnly = False
dt.Columns.Add(col)
col = New DataColumn
col.DataType = Type.GetType("System.String")
col.ColumnName = "Col3"
col.ReadOnly = False
dt.Columns.Add(col)
Dim dr As DataRow
For I As Int32 = 0 To 9
dr = dt.NewRow
dr("Col1") = I + 1
dr("Col2") = Chr(I + 65)
dr("Col3") = Chr(122 - I)
dt.Rows.Add(dr)
Next
dgrMain.DataContext = dt.DefaultView
End Sub
OnCellBeginEdit事件触发,但最终用户无法编辑单元格内容。为什么呢?