Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic verification of bindings
    text
    copied!<p>Or "<strong>how do you make sure all your bindings stay correct?</strong>"<br> <em>(this is kinda lengthy, but bear with me, I tried to make it as short as I could)</em></p> <p>Consider the following example:</p> <pre><code> &lt;TextBox Name="tb" /&gt; &lt;TextBlock Text="{Binding Text.TheProp, ElementName=tb}" /&gt; </code></pre> <p>It is perfectly known at compile time that the binding is incorrect (i.e. the parser knows the type of element <code>tb</code>, and therefore, it knows the type of it's <code>Text</code> property, and therefore, it knows that <code>TheProp</code> doesn't exist).<br> Yet, this code will compile and run (although with a binding error message in debug output).</p> <p>This behavior may come in very handy in some situations: no matter what exact type my data is, as long as it has appropriately named properties, I'm ok. Thus, we get sort of "declarative duck typing".</p> <p><strong>However</strong>, duck typing is not always a good thing.<br> Specifically, while using the MVVM pattern, I know (most of the time) the exact types of all my ViewModel objects. On the other hand, the models become more and more complex over time, which gets me worried about future refactoring: what if I decide to rename some properties, or, God forbid, put them in a separate aggregated object? What's going to happen with all my bindings then? Will I have to rake all XAML files by hand? And even without refactoring - what if I simply make a typo?</p> <p>A similar problem is already solved in other places of XAML. If, for instance, you put an incorrect property name in <code>Style/Setter/@Property</code>, you get a compile time error.<br> <code>TemplateBinding</code> also provides such verification. Which is very handy.</p> <p>So, ideally, I would love to see something like this:</p> <p><strong>ProductViewModel.cs:</strong></p> <pre><code> public class ProductViewModel { public Name { get; set; } public Price { get; set; } } </code></pre> <p><strong>ProductView.XAML:</strong></p> <pre><code> &lt;UserControl x:Class="Shopping.View.ProductView" x:DataContextType="vm:ProductViewModel" xmlns:vm="clr-namespace:Shopping.ViewModel" ... &gt; &lt;TextBox Text="{Binding Name}" /&gt; &lt;!-- OK --&gt; &lt;TextBox Text="{Binding Price}" /&gt; &lt;!-- OK --&gt; &lt;TextBox Text="{Binding ABC}" /&gt; &lt;!-- Compile time error: there is no property ABC in ProductViewModel --&gt; &lt;/UserControl&gt; </code></pre> <p><strong>ShoppingCart.XAML:</strong></p> <pre><code> &lt;UserControl x:Class="Shopping.View.ShoppingCartView" x:DataContextType="vm:ShoppingCartViewModel" xmlns:vm="clr-namespace:Shopping.ViewModel" ... &gt; &lt;ItemsControl ItemsSource="{Binding Products}" ItemType="vm:ProductViewModel" &gt; &lt;!-- Static check happens here ShoppingCartViewModel.Products must implement IEnumerable&lt;ProductViewModel&gt; --&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="vm:ProductViewModel"&gt; &lt;view:ProductView /&gt; &lt;!-- DataContext is known to be of correct type because of DataTemplate.DataType property --&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; &lt;/UserControl&gt; </code></pre> <p>But let's get back to reality. In reality, all that dreaming is just not going to happen in the near future.</p> <p>However, I am sure I'm not the first person to have this problem.<br> So, finally, the question is: <strong>How do you make sure your bindings are correct? And that they stay that way?</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload