Coder Social home page Coder Social logo

adhesive's Introduction

Adhesive

Adhesive gives you a multi-direction data binding for properties of objects that stick together. Adhesive is not intended to be used only for UI elements and can be used as method of connecting dissimilar APIs, etc.

Requirements

  • Any class who's member is used as the source for a binding, must implement INotifyPropertyChanged.
  • Properties should call OnPropertyChanged() when their value has been updated.
  • Properties must not call OnPropertyChanged() if they are set to the same value they already are (unless you don't intend on using TwoWayBindings).

Usage

All bindings, in one way or another, are some combination of a OneWayBinding internally.

Using the example classes:

public class Employee : INotifyPropertyChanged {

        private string _firstName;
        private string _lastName;

        public string FirstName {
            get => _firstName;
            set {
                if (_firstName == value) return;

                _firstName = value;
                OnPropertyChanged();
            }
        }

        public string LastName {
            get => _lastName;
            set {
                if (_lastName == value) return;

                _lastName = value;
                OnPropertyChanged();
            }
        }

        public Employee(string firstName, string lastName) {
            this.FirstName = firstName;
            this.LastName = lastName;
        }
        
        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null) {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }

    public class Nameplate {

        private string _inscribedName;

        public string InscribedName {
            get => _inscribedName;
            set {
                if (_inscribedName == value) return;

                _inscribedName = value;
                OnPropertyChanged();
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyName = null) {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }

OneWayBinding

var nameplateBinding = new Adhesive.OneWayBinding<string, string>(() => henrysNameplate.InscribedName, () => henry.FirstName);

Now, if henry.FirstName is set to any other value, henrysNameplate.InscribedName will immediately update to match it.

TwoWayBinding

var nameplateBinding = new Adhesive.TwoWayBinding<string, string>(
	() => henry.FirstName, 
	() => henrysNameplate.InscribedName, 
	 o => o.ToUpper(), 
	 o => o.ToLower(), 
	InitialBindingProcedure.ApplyRight
);

This will sync the values between henry.FirstName and henrysNameplate.InscribedName, but the value converters will ensure that henry.FirstName is always uppercase and henryNameplate.InscribedName is always lowercase.

ManyToOneBinding

var nameplateBinding = new Adhesive.ManyToOneBinding<string, string>(
	() => henrysNameplate.InscribedName,
	new List<Expression<Func<string>>>() {
		() => henry.FirstName,
		() => henry.LastName
	},
	 o => $"{henry.LastName}, {henry.FirstName}"
);

Updates to either henry.FirstName or henry.LastName will update henrysNameplate.InscribedName in the format "Lastname, Firstname."

OneToManyBinding

Sample Coming Soon

Allows for many properties to be updated when a single property is changed.

SyncBinding

Sample Coming Soon

All members are both source and target properties. A change to any property in the binding will be applied to all other members of the binding.

License

MIT

adhesive's People

Contributors

dlamkins avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.