Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

data binding problem - only updates on lostfocus

I have a combobox bound to a property in my custom class, which inherits INotifyPropertychanged.  when I change the value in the combobox (ie select a different value from the dropdown) you would expect it to change the corresponding bound property in my class value right away.  Unfortunately, this doesn't happen until the combobox loses focus.  I need it to update right after a new value is selected.  Why doesn't it do this, and how do i do it?  I would really like to avoid handling any combobox events such as SelectionChangeCommitted and 'hardcoding' this.

Any ideas?  Thanks
0
prosh0t
Asked:
prosh0t
1 Solution
 
existenz2Commented:
Actually the implementation this implementation is more logical then it sounds in the first place. I don't know if you use winforms or asp.net, but I'll give a winforms example. The asp.net example is basicly the same, but it just has some extra reasons why this implementation is correct.

Combobox with three values:
1. A
2. B
3. C

Your implementation:
The user selects #2 -> property gets updated (wrong value), but the user has selected accidently the wrong value, so he selects #3 -> again property update (correct value). This means the property is set twice in this scenario.

Then we got a worst case flow:
The user selects #2 -> property gets updated (wrong value), but the user has selected accidently the wrong value. An error occurs and the user can't do anything anymore. This makes it impossible to select the correct value (#3), but #2 is already set on the property which is invalid. This causes invalid data and that's what you don't want.

The .NET implementation:
The user selects #2, but the user has selected accidently the wrong value, so he selects #3 and he moves away (lostfocus for combobox) and the property is set. This means the property is set only once in this scenario.

Then we got a worst case flow:
The user selects #2 -> property, but the user has selected accidently the wrong value. An error occurs and the user can't do anything anymore. This makes it impossible to select the correct value (#3). But since the property wasn't automatically set in the first select. The original value is still available in the data, so the original data is still valid.



The best thing to do is actually relook at your design if this is really what you want (I don't know your scenario, but in most cases you don't want this) and else you will indeed need to use the events from the combobox to implement what you want. Using events is not bad, it actually is a very good method to use.

Hope this helps you :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now