[英]How to fix “unhandled exception” when clicking on WPF DataGrid?
I am using Visual Studio 2010, WPF with C# 4.0 and when clicking the cell in DataGrid I got the following exception: 我在C#4.0中使用Visual Studio 2010,WPF,并且在DataGrid中单击单元格时遇到以下异常:
An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll Additional information: A TwoWay or OneWayToSource binding cannot work on the read-only property 'Column2' of type 'VindecoderUI.AcesData'.
Here's the code I have: 这是我的代码:
<DataGrid AutoGenerateColumns="False" Height="190" Name="nadaDataGrid" Width="304" FrozenColumnCount="1000" ItemsSource="{Binding Source={StaticResource nadaDataCollection}}" CanUserReorderColumns="False" CanUserResizeColumns="True" CanUserSortColumns="False" AlternatingRowBackground="#3F000000" CanUserResizeRows="False" SelectionMode="Single" SelectionUnit="Cell"
SelectionChanged="dataGrid1_SelectionChanged" AreRowDetailsFrozen="True" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=NadaSeries}" Header="Nada Series" />
<DataGridTextColumn Binding="{Binding Path=NadaBS}" Header="Nada BS" />
<DataGridTextColumn Binding="{Binding Path=MSRP}" Header="MSRP" />
<DataGridTextColumn Binding="{Binding Path=GVWR}" Header="GVWR" />
<DataGridTextColumn Binding="{Binding Path=GCWR}" Header="GCWR" />
</DataGrid.Columns>
</DataGrid>
<Window.Resources>
<CollectionViewSource x:Key="nadaDataCollection"></CollectionViewSource>
<CollectionViewSource x:Key="acesDataCollection"></CollectionViewSource>
</Window.Resources>
By default, DataGrid
cells are editable (which means the bindings are TwoWay
.) Since you are binding a collection that includes items with a read-only property, you get an exception when you click on the cell, since editing couldn't work. 默认情况下,
DataGrid
单元格是可编辑的(这意味着绑定是TwoWay
。)由于您绑定的集合包含具有只读属性的项目,因此单击该单元格会出现异常,因为无法进行编辑。
You should be able to work around that by adding Mode=OneWay
to the column binding for that field. 您应该可以通过将
Mode=OneWay
添加到该字段的列绑定中来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.