简体   繁体   English

InvalidOperationException - TwoWay 或 OneWayToSource 绑定无法在只读属性上工作

[英]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.

相关问题 升级 .NET 版本后,TwoWay 或 OneWayToSource 绑定无法在只读属性上工作 - A TwoWay or OneWayToSource binding cannot work on the read-only property after upgrading the .NET version wpf datagrid单元格编辑错误:( TwoWay或OneWayToSource绑定不适用于只读属性) - wpf datagrid cell edit error : ( A TwoWay or OneWayToSource binding cannot work on the read-only property) 在自定义控件上进行OneWayToSource / TwoWay绑定 - Making a OneWayToSource/TwoWay binding on custom control DataTemplate 中只读属性的绑定更新 - Binding Update of a Read-Only Property in DataTemplate ListBoxItem和ListBoxContainer之间的IsSelected属性的TwoWay绑定导致意外行为。 OneWayToSource工作。 为什么? - TwoWay binding of IsSelected property between ListBoxItem and ListBoxContainer causes unexpected behaviour. OneWayToSource works. Why? 二维TwoWay绑定可以用作OneTime + OneWayToSource - 2-dimension TwoWay binding works as OneTime+OneWayToSource 将ComboBox TwoWay绑定到属性 - Binding ComboBox TwoWay to Property 只读依赖属性更新但在第一次使用时不起作用 - Read-only Dependency Property updates but does not work on first use Azure AD:属性“ mail”是只读的,无法设置 - Azure AD: Property 'mail' is read-only and cannot be set DataBinding具有只读属性 - DataBinding with read-only property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM