In the Model-View-ViewModel design pattern, we make frequent use of the INotifyPropertyChanged interface. It is the Interface that defines the necessary methods to Notify the UI when a Property has Changed. As you can see, its a very well-named Interface. 🙂
I’ve collected a few variations of the implementation and placed them all into a single ViewModelBase class. I didn’t write any of these myself – rather, I found them strewn about the internet. I’ve documented where I got some of them; others were (and probably still are) readily available from a multitude of sites. I’m making my collection available here, and I’ll give a few examples of how to use it. I’ll also explain the “evolution” my use of this interface has undergone.
To keep things organized, I usually create a folder named MVVM in my solution to house this class. You’ll want to update the namespace in this class to match your project; eventually I’m sure I’ll just compile it into a DLL once its evolution has ceased. Continue reading
One of the issues with multi-threading in WPF is the inability for processes on a separate thread from the user interface to directly access objects in the UI. The .NET Timer object does not run in the same thread as WPF’s UI, so it therefore cannot directly update the UI. Instead, the Timer must post UI updates to the dispatcher of the UI thread, using the Invoke or BeginInvoke methods. However, an easier method exists for implementing a Timer that updates the UI – the DispatcherTimer.
The DispatcherTimer runs on the same thread as the UI. Therefore, using the DispatcherTimer instead of the traditional Timer object allows UI updating without the need for the Invoke or BeginInvoke methods.
The DispatcherTimer works in almost the same way as the traditional Timer:
- Create an instance of the DispatcherTimer
- Assign a TimeSpan to the DispatcherTimer’s Interval property
- Assign an event handler to the DispatcherTimer’s Tick event
- Start the timer
It really is that easy! Let’s take a look at some sample code. All of the following code can be placed in your DataModel. Continue reading
I recently completed an application that made use of the WebClient object’s DownloadString method to obtain a JSON string. In my development environment retrieval of this data often took upwards of 30 seconds. If you’ve ever incorporated a time-consuming process in your application, you may have noticed that your user interface becomes unresponsive while the process is running.
Why does this happen? Because without special consideration, this time-consuming process is running on the same thread that handles updating of the UI. “How do I keep my UI responsive during a time-consuming process?”, you may ask. This can be easily accomplished through the use of the BackgroundWorker object. In this article, we’ll discuss the BackgroundWorker object, some of its available event handlers, and one of its methods. Continue reading
Recently my brother asked me if my daughters’ ages in my About Me page were static or if they would change as we moved through time. The answer is that they’re the result of PHP functions and are therefore dynamic and will change as time passes.
You see, my site allows me to embed PHP code. In a nutshell, PHP allows me to create HTML dynamically. I wrote a PHP function that calculates the number of years/months (or months/days in certain situations) since a given birthdate. I added this function to my site’s custom functions.php file, and registered it with the WordPress framework so that I can access it with a shortcode. Because I want to be able to use this function multiple times with various birthdates, I wrote it to accept an argument called “birthdate”. In my About Me page, anytime I want somebody’s age, I simply insert:
I was in need of a WPF color picker control and settled on the Extended WPF Toolkit. Note that I didn’t say that I settled for – simply put, the controls in this collection are amazing for the price.
I’m working on a project developing an application for a touch-screen interface. As such, all of my screen elements are BIG, to accommodate navigation via touch. It occurred to me that I could save space by eliminating labels for textboxes. Of course, I don’t really want to have a bunch of textboxes with no way of knowing which textbox is for which bit of data. I wanted a way to have the textbox display default text when it’s empty, but to allow for otherwise normal input. I chose to implement both the OnTextChanged and PreviewTextInput methods to accommodate this functionality.