By now you all probably have heard all of the latest and greatest things that are coming with Windows 8.1. If you weren’t at Build 2013, make sure you head over to Channel 9 and see what you missed. If you have been keeping up with it, I’m sure there are things that you are excited to see and things that you weren’t happy that are still missing. But hey, you do have to admit it’s an awfully quick release cycle.
It’s been a while since I did a blog series, so I thought it was about time for another. So I’m going to do a series on my 10 favorite XAML additions to Windows 8.1. Now, to be fair, there are a lot of other things that I like about the release, but these are just the top 10. I’m sure there will be a lot more making it’s way to this blog. So make sure you come back as this series unfolds over the next couple of weeks.
If you have heard any of my talks on XAML or even just had a discussion with me on the topic, you know I love the data binding in XAML. To see so many failed attempts over the year, it’s awesome when something is just done right. This data binding engine has been a part of each platform that XAML has made an appearance. Windows 8 is no exception. In fact, one of the things I like best about the Windows 8 approach is that its now all native. However, with the revamping of the engine came some missing features that didn’t make the original cut. Fortunately, several of these things have shown back up in Windows 8.1 and I thought this would be a great way to start off the series.
The first thing that you will come across is the Binding class has a few more properties. The first being the TargetNullValue property. TargetNullValue allows you to provide a default value when the source value is NULL. This is a great way to display default information or to show some missing data. The value for TaretNullValue can be a static value or string, however, it can also refer to a StaticResource or another Binding.
The FallbackValue is similar to the TargetNullValue property. The difference is the TargetNullValue is used when the binding property is NULL and the FallbackValue is used when the binding property does not exist. So if you map a binding to the “LastName” property and the object in the DataContext does not have a “LastName” property, the FallbackValue will be used. As with the TargetNullValue, you can set this value to another Binding statement.
Finally you have the UpdateSourceTrigger property. By default, a binding updates anytime the PropertyChanged event is raised for the property. The UpdateSourceTrigger allows you to modify that behavior. The UpdateSourceTrigger property is actually an enum of the same name, UpdateSourceTrigger, which has three values: Default, PropertyChanged, and Explicit. Default and PropertyChanged provide the same functionality and the Default is there for backwards compatability. Setting the value to Explicit means the binding won’t update until trigger it in code.
In order to explicitly update the binding you need access to the BindingExpression for the item. The FrameworkElement now has a GetBindingExpression method that returns a BindingExpression instance for the supplied DependencyProperty. The BindingExpression has an UpdateSource method will allow you to manually update the binding. The following shows an example of this in C#.
var expression = tbName.GetBindingExpression(TextBox.TextProperty); expression.UpdateSource();
Another addition to the FrameworkElement is the DataContextChanged event. For Silverlight developers, this was a missing feature from Silverlight 5. With the DataContextChanged event you can be informed any time a new source is assigned to the DataContext property. This comes in handy when you need to do any manual UI updating when a new source is assigned.