Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Two-way binding modifies value of read-only property #1379

Closed
pavelovcharov opened this issue Jul 25, 2019 · 0 comments
Closed

Two-way binding modifies value of read-only property #1379

pavelovcharov opened this issue Jul 25, 2019 · 0 comments
Labels
Bug Product bug (most likely) rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release tell-mode Issues and PR's that require notice to .NET Core Shiproom
Milestone

Comments

@pavelovcharov
Copy link

  • .NET Core Version: 3.0.100-preview7-012821
  • Windows version: 1903 (18262.53)
  • Does the bug reproduce also in WPF for .NET Framework 4.8?: No

Problem description:
The two-way binding now changes properties with non-public setters. Is this the expected behavior? .NET Framework 472 throws InvalidOperationException, when you're trying to set two-way binding to read-only property:

System.InvalidOperationException: 'A TwoWay or OneWayToSource binding cannot work on the read-only property 'ReadonlyProperty' of type 'Test.ObjectWithReadonlyProperty'.'

Actual behavior:
The two way binding modifies property value even if it has a non-public setter
Expected behavior:
The two way binding shouldn't modify read-only properties. The InvalidOperationException should be thrown.
Minimal repro:

   class ObjectWithReadonlyProperty : INotifyPropertyChanged {
       string readonlyProperty;
       public ObjectWithReadonlyProperty(string v) {
           readonlyProperty = v;
       }
       public string ReadonlyProperty { get { return readonlyProperty; } private set { readonlyProperty = value; } }
       public event PropertyChangedEventHandler PropertyChanged;
       public void RaisePropertyChanged() {
           PropertyChanged(this, new PropertyChangedEventArgs(null)); //-V3083
       }
   }
   [TestFixture]
   public class TestFixture {
       [Test]
       public void TwoWayBindingToReadonlyProperty() {
           var btn = new Button();
           var data = new ObjectWithReadonlyProperty("12345");
           BindingOperations.SetBinding(btn, Button.ContentProperty, new Binding(nameof(ObjectWithReadonlyProperty.ReadonlyProperty)) { Mode = BindingMode.TwoWay, Source = data });
           Assert.AreEqual("12345", btn.Content);
           btn.Content = "67890";
           Assert.AreEqual("12345", data.ReadonlyProperty);
       }
   }
@grubioe grubioe added this to the 3.0 milestone Jul 26, 2019
@grubioe grubioe added Bug Product bug (most likely) rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release labels Jul 26, 2019
@vatsan-madhavan vatsan-madhavan added the tell-mode Issues and PR's that require notice to .NET Core Shiproom label Aug 3, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Apr 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Product bug (most likely) rank20 Rank: Priority/rank on a scale of (1..100) regression status: This issue is a regression from a previous build or release tell-mode Issues and PR's that require notice to .NET Core Shiproom
Projects
None yet
Development

No branches or pull requests

4 participants