Databinding the videogame, part 3: actual databinding

Last time, I mapped terrain objects to pretty pictures — but only one terrain square at a time. And despite the title, it wasn’t really databinding as such.

This time, I fix one of those two problems… the databinding one. ’Cause it turns out it’s not hard — I just needed to figure out the right way to do it. And of course, I figured it out after I had already posted part 2.

Databinding happens through the magic of the {Binding ...} extension syntax in XAML. (Yeah, you can also do it in code, but I don’t need that today.) I already knew about some of the settings you could put between the braces; for example, {Binding Path=PropertyName} will databind to the property named PropertyName on whatever object is in the DataContext. But I couldn’t figure out how to bind my control’s Content to the DataContext itself.

Well, somehow it hadn’t occurred to me that I could just use {Binding} by itself, without any parameters inside the curly braces. But of course you can, and poof — whatever property you used {Binding} on is thenceforth bound to the DataContext, and changes whenever the DataContext changes. Once I finally figured that out, the switch to databinding was just a three-line diff (well, okay, four if you’re being picky — there was a line break):

MapView.xaml:

@@ -3,7 +3,8 @@
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:TerrainDataModels="clr-namespace:Game.Core.TerrainDataModels"
     xmlns:TerrainViews="clr-namespace:Game.Core.TerrainViews"
-    MinWidth="40" MinHeight="40" Background="Gray">
+    MinWidth="40" MinHeight="40" Background="Gray"
+    Content="{Binding}">
     <UserControl.Resources>
         <DataTemplate DataType="{x:Type TerrainDataModels:Dirt}">
             <TerrainViews:DirtView/>

Window1.xaml.cs:

@@ -15,11 +15,11 @@
 
         private void Dirt_Click(object sender, RoutedEventArgs e)
         {
-            mapView.Content = new Dirt();
+            mapView.DataContext = new Dirt();
         }
         private void SteelPlate_Click(object sender, RoutedEventArgs e)
         {
-            mapView.Content = new SteelPlate();
+            mapView.DataContext = new SteelPlate();
         }
     }
 }

Voilà — databinding. Of course, it still does the exact same things as before, but at least I made the title honest.

Today’s test project

Today’s project is the same as yesterday’s, but with real databinding. You can browse today’s code online, or check it out from my Subversion repository:

svn co http://svn.excastle.com/databinding_the_videogame/tags/part3

What’s next?

I’m thinking the next thing is to bind to a whole collection of terrain objects.

Yes, I know that’s what I said last time. But I’m learning this WPF stuff as I go, and on the off-chance somebody might someday stumble on this while they’re learning WPF, I think it’s worth blogging in small increments. This post is about binding a single object, which is useful info in its own right. The next one will be about binding a collection. I’m blogging in small commits.

Leave a Reply

Your email address will not be published. Required fields are marked *