Solved

INotifyPropertyChanged property name proglem

Posted on 2009-05-18
6
803 Views
Last Modified: 2013-12-17
I've got a problem with a PropertyChanged event been ignore when DataBinding. The property in question is called "Property" which leads me to believe that it could be a reserved name. However I've found no mention in the documentation of this being the case.

When I call EndEdit on the BindingSource, the changes to Property are ignored, but the changes to Property1 are retained. The PropertyChanged event for "Property" is definitely being raised.

Also note that the databinding will display the initial value correctly. It's only the updating that doesn't work

The work around is of course to change the name to something other than "Property", a bit annoying as Property is the exact description of what is contained (a segment of an address), and is what is used in a 3rd party address resolving tool.

Basically, maximum points to anyone who can point me to where it says I'm not allowed to databind to a property called "Property", and why this fails silently rather than throwing an exception.

I've attached a fully working example to demonstrate what happens. Just compile using:
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe Form1.txt
BindingSource source;

    MyControl myControl = new MyControl();
 

    private void Form1_Load(object sender, EventArgs e) {

      this.Controls.Add(myControl);

      source = new BindingSource();
 

      DataTable table = new DataTable();

      table.Columns.Add("fldProperty", typeof(decimal));

      table.Columns.Add("fldProperty1", typeof(decimal));

      table.Rows.Add(0, 0);

      table.Rows.Add(10, 10);

      table.Rows.Add(20, 20);

      table.AcceptChanges();

      source.DataSource = table;
 

      dataGridView1.DataSource = source;

      myControl.DataBindings.Add("Property", source, "fldProperty");

      myControl.DataBindings.Add("Property1", source, "fldProperty1");

    }
 
 

    private void button1_Click(object sender, EventArgs e) {

      myControl.Property++;

      myControl.Property1++;

      source.EndEdit();

    }
 

// Snip
 

  public class MyControl : Control, INotifyPropertyChanged {

    private decimal property, property1;
 

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
 

    protected void OnPropertyChanged(string propertyName) {

      if (PropertyChanged != null)

        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }

    #endregion
 

    public decimal Property {

      get { return property; }

      set { 

        property = value;

        OnPropertyChanged("Property");

      }

    }
 

    public decimal Property1 {

      get { return property1; }

      set {

        property1 = value;

        OnPropertyChanged("Property1");

      }

    }

  }

Open in new window

Form1.txt
0
Comment
Question by:oobayly
  • 4
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 24417375
I tried changing the name to lowercase "property" and it worked.

I think you have stumbled upon an "interesting" bug.

I notice in this book snippet the same thing
http://books.google.com/books?id=F_q6Rcc-8yIC&pg=PA250&lpg=PA250&dq=DataBindings+Add("Property")&source=bl&ots=LpBBLm2kb4&sig=nPJDRIJ7UxFBuDpVs1q7u2PSXeg&hl=en&ei=pe0RSqfwFZ_ItAPrlMjtDQ&sa=X&oi=book_result&ct=result&resnum=4



0
 
LVL 15

Author Comment

by:oobayly
ID: 24420253
Bah, I'd hoped that somebody wouldn't use the B-word :-)
Going to post a bug report on connect.microsoft.com (not that it will do much). When I've posted the bug report, I'll post the link here as well.

As for changing the name to lowercase, it makes sense as Reflection is case sensitive by default, so it has the same effect as changing the name.
0
 
LVL 15

Author Comment

by:oobayly
ID: 24429846
I've posted a bug report at Microsoft Connect:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=454486

If you don't mind, I'm going to leave this open until MS get back with a response. If it looks like a bug, acts like a bug and quacks like a bug :-), then it's probably a bug. I'm just a glass is half full kind of person, so I'm hanging on with a shred of hope.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 12

Expert Comment

by:williamcampbell
ID: 24433717
Let it go, Move on :)
0
 
LVL 15

Author Comment

by:oobayly
ID: 24439348
But it just feels like it's too soon to move on. :-)
0
 
LVL 15

Author Closing Comment

by:oobayly
ID: 31582696
Seeing as you confirmed my suspicion that it's a bug.
However, I'll post any follow ups from Microsoft Connect.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DataGridView Events ? 3 37
Replace &lt; with < 14 55
Entity Framework 7 31
C# MVC and a global static factory that never dies 4 46
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now