bind_property


Description:

[ CCode ( cname = "g_object_bind_property_with_closures" ) ]
[ Version ( since = "2.26" ) ]
public unowned Binding bind_property (string source_property, Object target, string target_property, BindingFlags flags = DEFAULT, owned BindingTransformFunc? transform_to = null, owned BindingTransformFunc? transform_from = null)

Creates a binding between source_property on this and target_property on target, allowing you to set the transformation functions to be used by the binding.

This function is the language bindings friendly version of g_object_bind_property_full, using Closures instead of function pointers.

Example: Property bindings:

public class ObjectA : Object {
public int property_a { get; set; }
public int property_b { get; set; }
public int property_c { get; set; }
public bool property_d { get; set; }
public int property_e { get; set; }
}

public class ObjectB : Object {
public int property_a { get; set; }
public int property_b { get; set; }
public int property_c { get; set; }
public bool property_d { get; set; }
public int property_e { get; set; }
}

public static int main (string[] args) {
// Objects:
ObjectA obja = new ObjectA ();
ObjectB objb = new ObjectB ();


//
// BindingFlags.DEFAULT:
//

print ("BindingFlags.DEFAULT:\n");

obja.property_a = 50;
objb.property_a = 60;

obja.bind_property ("property-a", objb, "property-a", BindingFlags.DEFAULT);

// Output: ``50 - 60``
print (" %d - %d\n", obja.property_a, objb.property_a);

// Output: ``10 - 10``
obja.property_a = 10;
print (" %d - %d\n", obja.property_a, objb.property_a);

// Output: ``10 - 15``
objb.property_a = 15;
print (" %d - %d\n", obja.property_a, objb.property_a);


//
// BindingFlags.BIDIRECTIONAL:
//

print ("BindingFlags.BIDIRECTIONAL:\n");

obja.property_b = 50;
objb.property_b = 60;

obja.bind_property ("property-b", objb, "property-b", BindingFlags.BIDIRECTIONAL);

// Output: ``50 - 60``
print (" %d - %d\n", obja.property_b, objb.property_b);

// Output: ``10 - 10``
obja.property_b = 10;
print (" %d - %d\n", obja.property_b, objb.property_b);

// Output: ``15 - 15``
objb.property_b = 15;
print (" %d - %d\n", obja.property_b, objb.property_b);


//
// BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL:
//

print ("BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL:\n");

obja.property_c = 50;
objb.property_c = 60;

obja.bind_property ("property-c", objb, "property-c", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL);

// Output: ``50 - 50``
print (" %d - %d\n", obja.property_c, objb.property_c);

// Output: ``10 - 10``
obja.property_c = 10;
print (" %d - %d\n", obja.property_c, objb.property_c);

// Output: ``20 - 20``
objb.property_c = 20;
print (" %d - %d\n", obja.property_c, objb.property_c);


//
// BindingFlags.INVERT_BOOLEAN:
//

print ("BindingFlags.INVERT_BOOLEAN:\n");

obja.property_d = false;
objb.property_d = true;

obja.bind_property ("property-d", objb, "property-d", BindingFlags.INVERT_BOOLEAN);

// Output: ``true - false``
obja.property_d = true;
print (" %s - %s\n", obja.property_d.to_string (), objb.property_d.to_string ());

// Output: ``false - true``
obja.property_d = false;
print (" %s - %s\n", obja.property_d.to_string (), objb.property_d.to_string ());


//
// Transformer:
//

print ("Transformer:\n");

obja.bind_property ("property-e", objb, "property-e", BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL, (binding, srcval, ref targetval) => {
int src = (int) srcval;
targetval.set_int (src * 2);
return true;
}, (binding, srcval, ref targetval) => {
int src = (int) srcval;
targetval.set_int (src / 2);
return true;
});


obja.property_e = 50;
objb.property_e = 60;

// Output: ``30 - 60``
print (" %d - %d\n", obja.property_e, objb.property_e);

// Output: ``10 - 20``
obja.property_e = 10;
print (" %d - %d\n", obja.property_e, objb.property_e);

// Output: ``20 - 40``
objb.property_e = 40;
print (" %d - %d\n", obja.property_e, objb.property_e);

return 0;
}

valac --pkg gobject-2.0 GLib.Object.bind_property.vala

Parameters:

this

the source Object

source_property

the property on this to bind

target

the target Object

target_property

the property on target to bind

flags

flags to pass to Binding

transform_to

a Closure wrapping the transformation function from the this to the target, or null to use the default

transform_from

a Closure wrapping the transformation function from the target to the this, or null to use the default

Returns:

the Binding instance representing the binding between the two Object instances. The binding is released whenever the Binding reference count reaches zero.




2022 vala-language.org