Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate data binding in XAML in compile time

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:

  1. App developer writes property NmberOfApples and unit tests to check its correct behavior
  2. UI developer creates user control and bind it to the property
  3. App developer finds that property has misspelling and fix its name to NumberOfApples
  4. It would be compilation time errors in any C# code uses NmberOfApples property, and such errors will be easy to catch (Continuous Integration)
  5. Data binding in XAML files are not going to be validated and it will be run time error

My question will be “Is there any tool or methodology that help us validate data binding correctness in XAML in compile time?”

like image 290
db_ Avatar asked Dec 10 '09 23:12

db_


People also ask

What is data binding in XAML?

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.

What is two way binding XAML?

TwoWay binding causes changes to either the source property or the target property to automatically update the other.

What is the data binding concept and how binding works in WPF?

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.

What is binding mode in WPF?

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.


1 Answers

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.

like image 191
Jose Avatar answered Oct 05 '22 11:10

Jose