InvalidOperationException - TwoWay 或 OneWayToSource 绑定无法在只读属性上工作 [英] InvalidOperationException - A TwoWay or OneWayToSource binding cannot work on the read-only property
问题描述
我正在使用 MVVM 模式并在运行我的应用程序时收到以下信息
I'm using the MVVM pattern and am receiving the following when i run my app
无效操作异常TwoWay 或 OneWayToSource 绑定无法对ViewModel.SynergyViewModel"类型的只读属性Options"起作用.
InvalidOperationException A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.SynergyViewModel'.
我已经在我的视图模型中注释了我的所有源代码,并将其追溯到一个复选框.如果我注释掉应用程序运行的视图模型中的复选框或属性,减去功能.下面我列出了我的复选框的代码和视图模型中的属性.
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.InvalidOperationExceptionMessage=A TwoWay 或 OneWayToSource 绑定无法对ViewModel.MyViewModel"类型的只读属性Options"起作用.来源=演示框架堆栈跟踪:在 MS.Internal.Data.PropertyPathWorker.CheckReadOnly(对象项目,对象信息)内部异常:
System.InvalidOperationException occurred Message=A TwoWay or OneWayToSource binding cannot work on the read-only property 'Options' of type 'ViewModel.MyViewModel'. Source=PresentationFramework StackTrace: at 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
.
Either make your setter public or explicitly set the Binding.Mode
to OneWay
.
这篇关于InvalidOperationException - TwoWay 或 OneWayToSource 绑定无法在只读属性上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!