I intend to use data binding between a few of my classes. In other words, I am not binding values between a model class and the UI, but to bind variables between different classes.
I have read about data binding in C# on several places, but most of them are referring to binding between Windows Form and a source object.
I am still new to C#. This is how I understand what I should do:
First, for my source object, say has a class name of DataObject
. The source object has to implement a INotifyPropertyChange
interface, and then trigger the event whenever the property, health
, is set to change. I have no problem with this.
Now, suppose I have a target object called CharacterClass
. life
is a property in CharacterClass
, and is also the target property that I want to bind to the source object's health
property.
How can I bind the two properties together (both one-way and two-way) in code with only just the ordinary .NET framework?
A bit of background information on why I ask this question:
Just in case you think this is a duplicated question, it's not. I have searched through SE. The other questions on databinding in code are in the context of WPF or XAML, which is not for me. I have also read several articles on MSDN and it seems that I could create a Binding
object, and then bind the source and target via BindingOperations.SetBinding()
. However, the Binding
class seems to be part of the WPF library under the namespace of System.Windows.Data.Binding
. Although I am using C#, I doubt I would have the luxury to access to WPF libraries because I'm mainly using C# as only a scripting language within Unity3D. I believe I only have access to the the vanilla .Net framework. But, I am not very sure about this because I am still new to C#.