A weird glitch in MVVM’s Data Binding

This time I’m complaining about an obvious glitch that kept throwing me off for some hours. Before I talk about it, I must state that I really love MVVM as a pattern, because it is such a ginormous improvement above the classic MVC-pattern, which I honestly hated instantly when I began developing. Sure I used it, but I never liked it.

So far with history, let’s start with the main article.

Assume you got a ViewModel that you have bound to the DataContext of your control, one property that is used for DataBinding looks like this:

We got a text control in our application, plus a ribbon instead of a classic menu bar. The ribbon’s “Bold” toggle button (like in MS Word) has its “IsChecked” property bound to the above property. Whenever the user clicks the button, the currently selected text will be bolded, and unbolded if he clicks again. Also, the button’s state will now change its state according to the format the text got at the current’s cursor location (done with the INotifyPropertyChanged‘s only method that forces WPF to update the bound properties).

So far, so standard and working.

But, imaginge if the above property had no setter (just a getter), and the binding looked like this (pay attention to the highlighted portion):

Please remember to remove the setter from your above property (as described above). Then run the application, wait for an “OnPropertyChanged()” method to be called for that property, and visit how it sometimes won’t work at all – the button will stay as it was or it will behave unpredictably. Please also note that this behavior might often appear, but sometimes don’t. I and my workmate can confirm this behavior.

The workaround is simple: Remove the “Mode=OneWay” statement and put an empty setter into the desired property. And voila – it will work as intended.

I think this is a problem that is unnecessary to be in WPF, since solving this problem took me at least one hour.

I can’t think of a better explanation than this: Whenever you use “Mode=OneWay“, the mode isn’t really One-Way unless you got at least the Get and Set-Methods in your property. If you haven’t got the Set-Method, then the One-Way won’t work. Why is that? Actually I don’t know. I haven’t disassembled the WPF-Framework’s source, and I won’t do it just for that, but I think it is an error afterall and worth noting.

It’s interesting – right now I’ve set up a mock test project with just a viewmodel, a button, a checkbox, and a property called “chkChecked”.

The button updates the ViewModel’s “chkChecked” property which in turn will raise the OnPropertyChanged() method which eventually updates the checkbox. And guess what – it worked! That’s somehow random, because – as I’ve told earlier in this article – it didn’t work the same way with the ribbon! Something mystic is going on here… but for now, I know how to circumvent rare cases when MVVM seems to throw you off 😉

Have a nice day!

chris

ginormous

Posted in Uncategorized | Leave a comment

One annoying thing about .Net

If you have read my “About” page, then you know that I really like C# and .Net in any aspect.

But one thing that I still can’t understand is how Microsoft is so fixated on Windows with .Net.

Sure, there do exist alternatives on Linux (Mono for example), but the point I’m making is: Why doesn’t Microsoft port the Framework themselves to Linux and Mac OS X? Sure, the percentage of Mac and Linux users is still (obviously) not big enhough for Microsoft that would make the port be financially reasonable, but in my opinion its like with Java: Java wouldn’t be that big of a deal today if it would be fixated on – let’s say – only the Mac OS.

As for myself, I really like to see how enthusiats try their best to port .Net to Linux, and Microsoft should really support that team, because they are doing the work that I think Microsoft should do (or maybe that was intended? We never know).

Also I believe that the above fact is the reason that not as much Open-Source-Software is using .Net as Java,  because Java has the advantage that it runs on any OS without recompilation (as stated above).

.Net would deserve the same.

Posted in .Net | Tagged , , , | Leave a comment

Hello world!

Welcome on my blog! Here, I will post some news about projects I’m working on, some things I need to comment on, or just some random stuff. It’s like you never know what comes next, not even me 😉 So thank you for reading in advance!

Posted in Uncategorized | Leave a comment