我是一名非英语母语者,因此我的英语不太好。如果我在使用语言方面犯了错误,请谅解 :)
我是C#和WPF的新手,正在尝试将WPF DataGrid双向绑定到DataTable。现在,当我在DataGrid中编辑值时,DataTable中的数据会正确更改。但是,当我尝试使用以下代码填充DataTable时:
OleDbDataAdapter adapter = new OleDbDataAdapter("a query", (a connection));
adapter.Fill(dataTable);
代码可以运行,DataGrid看起来也没问题。但是当我尝试这样做时:
dataTable.Rows[0][1] = (some object);
显示值不会改变。我尝试以以下方式检查DataGrid中的值:
MessageBox.Show((SomeDataGrid.Items[0] as DataRowView).Row[1].ToString());
没问题,看来一切正常。我想知道为什么显示值是这样的。
这是我的 XAML 中的 DataGrid.CellStyle:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="DataGridCell">
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="{TemplateBinding Background}">
<DataGridDetailsPresenter HorizontalAlignment="Stretch" VerticalAlignment="Center" Content="{TemplateBinding Content}"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<!-- triggers -->
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
我一直在这个问题上卡了好几天了,谢谢任何帮助!