[英]InvalidOperationException - A TwoWay or OneWayToSource binding cannot work on the read-only property
I'm using the MVVM pattern and am receiving the following when i run my app我正在使用 MVVM 模式,并在运行我的应用程序时收到以下信息
InvalidOperationException A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.SynergyViewModel'. InvalidOperationException TwoWay 或 OneWayToSource 绑定无法对“ViewModel.SynergyViewModel”类型的只读属性“Options”起作用。
I have commented all my source out in my view model and have traced this back to a check box.我已经在我的视图模型中注释了我的所有源代码,并将其追溯到一个复选框。 If i comment out the the checkbox or the properity in my view model the app runs, minus the functionality.如果我注释掉应用程序运行的视图模型中的复选框或属性,减去功能。 Below i have listed the code for my checkbox and the property within the viewmodel.下面我列出了我的复选框的代码和视图模型中的属性。
<CheckBox Grid.Column="4" HorizontalAlignment="Right" Margin="5,0,5,5" IsChecked="{Binding Options}" Content="Options"/>
private bool _Options;
public bool Options
{
get
{
return _Options;
}
private set
{
if (_Options == value)
return;
_Options = value;
OnPropertyChanged("Options");
}
}
System.InvalidOperationException occurred Message=A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.MyViewModel'. System.InvalidOperationException 发生 Message=A TwoWay 或 OneWayToSource 绑定无法对“ViewModel.MyViewModel”类型的只读属性“Options”起作用。 Source=PresentationFramework StackTrace: at MS.Internal.Data.PropertyPathWorker.CheckReadOnly(Object item, Object info) InnerException: Source=PresentationFramework StackTrace: 在 MS.Internal.Data.PropertyPathWorker.CheckReadOnly(Object item, Object info) InnerException:
Any ideas on what i'm what i'm missing here?关于我在这里缺少什么的任何想法?
要么让你的 setter 公开,要么明确地将Binding.Mode
设置为OneWay
。
你的 setter 是私有的,要么将绑定指定为 OneWay 模式,要么从 setter 中删除私有
In my absolutely stupid case, I have forgotten to define a setter for a property, making it, well, read-only.在我绝对愚蠢的情况下,我忘记为属性定义一个 setter,使它成为只读的。 Just my 2 cents for those who work too late.对于那些工作太晚的人,只需我的 2 美分。
For those who find this without using PropertyChanged
对于那些在不使用PropertyChanged
情况下发现这一点的人
Regardless of whether PropertyChanged
is used, this exception is also thrown when you have a calculated property (without setter) and the user tries to edit the column.无论是否使用PropertyChanged
,当您有计算属性(没有 setter)并且用户尝试编辑该列时,也会抛出此异常。 Setting the whole DataGrid to IsReadOnly="True"
or just the column to ReadOnly is enough then.将整个 DataGrid 设置为IsReadOnly="True"
或仅将列设置为 ReadOnly 就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.