Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking UI objects together : could I do it with attributes?

I'm currently working on a custom UI engine for a game. I've very basic C# skills (I'm a C programmer by trade) and I'm having problems figuring out how to link a UI element with the data it represents. Here's what I mean:

class SomeClass
{
    //some stuff

    //This is what I want to monitor and/or modify.
    private SomeType _myVariable;
}

Elsewhere...

class UITextboxWidget
{
     //methods and stuff
}

Now, what I want is for the _myVariable element to be modified when the users enters a value in the textbox changes, and vice versa.

I first tried doing it by calling a method in the MyClass constructor and passing a Property on _myVariable by reference but I quickly discovered that this is not possible in C#.

Then I had a look at attributes and discovered you can create new ones : this is what I had in mind.

class MyClass
{
    [TextBoxAttribute(some, parameters)]
    private SomeType _myVariable;
}

That would make the embedding in the UI system very easy, but I'm not quite sure if it is possible to use attributes in this manner.

So, simply put:

  1. Can I do this with attributes and if so, how?
  2. If not, what would be a good way of doing this?
like image 590
SolarBear Avatar asked Dec 19 '25 21:12

SolarBear


1 Answers

What you want to do is called 'Databinding'. Specificly in your situation, it is Object Data Binding. Follow this link and it will get you started.

like image 156
Teoman Soygul Avatar answered Dec 22 '25 10:12

Teoman Soygul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!