Data Binding

With my Warspace game, i’ve separated the code into two categories. First there are the xaml files, and the c# code files for those xaml files. They are responsible for displaying information on the screen to the user. Then I have all the game objects and game logic files, which keep track of all the planets, the buildings and resources on the planets.

Objects which require it have an update method. This update method takes care of calculating (every second) things like how many resources are being produced on a planet, or how much power is being used by the buildings on the planet. One thing I had to have is the display be updating with the information in the game objects in real time. So for example, if a building has finished being built, the display would show the change. Previously I had a few update method for the display, that the Game Objects could call (via a Handler) to tell the display to update.

However this was quite tricky, it was a lot of work as I had to write update methods for everything they could be changed, and I could see that wasn’t going to work. Fortunately WPF has data binding, which I didn’t really understand at the start. I searched lots of websites and looked at examples, but a friend explained to me what I wanted and I got it working.

dataBindings.Add("Power", new Binding());
dataBindings["Power"].Source = planet; 
dataBindings["Power"].Path = new PropertyPath("Power"); 
txtPower.SetBinding(TextBlock.TextProperty, dataBindings["Power"]);

This is a bit of the code for the Planet Manager window, showing the binding for the Power text, which is how much power is being generated on the planet. Because I had to have lots of different bindings for different elements on the display, I made a Dictionary to store them. This allowed me to simply make a new binding whenever I need it, and refer to it by using a string (in this case “Power”) The source of the binding is a ‘planet’ object, and the Path refers to which property of the ‘planet’ this binding is for.

In order for a game object to be used this way, it need to implement the interface ‘INotifyPropertyChanged’ like so;

public class Planet : IUpdatable, INotifyPropertyChanged 
  public event PropertyChangedEventHandler PropertyChanged;
  void OnPropertyChanged(string prop)
  if (PropertyChanged != null)
  PropertyChanged(this, new PropertyChangedEventArgs(prop)); 

Now then a Property can be defined like this;

public int Power 
 get { return power; } 
 power = value ;

Each time the ‘Power’ Property is set, it calls the ‘OnPropertyChanged’ method, passing a string saying which Property has been changed. The Binding for the Power Textblock in the display is notified of the change and then the Textblock shows the new value.


All the elements here, like the building percentages, building level and power using bindings to reflect the values in the planet object.

This entry was posted in Game Development and tagged , . Bookmark the permalink.

Leave a Reply

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