I’m working on WPF based application. Environment is VS2008 SP1 with .NET 3.5 SP 1. In our development we are using MVVM pattern widely.
I.e. application developers write Models and ViewModels (C#), then UI developers will write Views using WPF Binding (XAML). Application developers also write unit tests on top of ViewModels. We are using Continuous Integration methodology and we are diond build and executing unit test on each modification
The problem is a lack of process or tools of data binding correctness validation in XAML. For example:
My question will be “Is there any tool or methodology that help us validate data binding correctness in XAML in compile time?”
Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.
TwoWay binding causes changes to either the source property or the target property to automatically update the other.
Data binding is a mechanism in WPF applications that provides a simple and easy way for Windows Runtime apps to display and interact with data. In this mechanism, the management of data is entirely separated from the way data. Data binding allows the flow of data between UI elements and data object on user interface.
Default Data-bindingIt just defines which is the default binding mode for the control's property. In WPF different controls has different default data-binding modes. For example, TextBlock's Text property has one-way as default binding mode but a TextBox's Text property has a two-way binding mode.
A solution to your problem is discussed in this article.
The basic idea is to create a ViewModel MetaData set of static(c#) classes that hold the string value of the properties of your ViewModel classes which you can then use in your xaml. The article explains how to use T4 text generation to create these static metadata classes. You could use any code generation tool of your preference.
so your VM has the following:
namespace Mine
{
public class MyViewModel
{
public int MyInt {get;set;}
public string MyString {get;set;}
}
}
And you code generation would create this:
namespace Mine.MetaData
{
public static class MyViewModelMetaData
{
public const string MyInt = "MyInt";
public const string MyString = "MyString";
}
}
and then in your xaml you would add the namespace to your xaml and bind your controls to the metadata class
<TextBox Text="{Binding Path={x:Static Metadata:MyViewModelMetadata.MyInt}}"/>
If you use an add-in like resharper then it will give you intellisense on the properties of the static class and also because you are referencing an exact property in a static class, when the static class gets regenerated your xaml should not compile.
It's pretty slick, I think it's awesome and it has the chance of keeping most people sane, but your mileage may vary. :)
EDIT:
By the way, I don't buy the "ViewModels are tightly coupled to the Views". In my opinion Views are inextricably bound to their ViewModels, but it should only be one way. ViewModels should be completely independent of any view implementation. It's like the ViewModel is the interface and the View is the concrete implemented class. So for this reason I don't put in any WPF-specific properties(e.g. Visibility enumeration) into my ViewModel because that binds me to use WPF for eternity(which isn't really a bad thing :) ), but it compromises maintenance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With