如何有条件地获得bind_property()的两个属性?

How to conditionally bind_property () two properties?

好久没解决这个问题了。这是一个练习,其中 Spin Button 和 Scale 在勾选检查按钮时不断同步。但是,当复选按钮未激活时,缩放和旋转都可以自由获取任何值。

在上一个问题 here 中,有人建议使用 bind_property 方法,当您希望始终同步旋转和比例时,该方法非常有效。但是,当需要 link 它到 activity (或缺少)复选按钮时,它不起作用。第二次点击勾选按钮,程序挂了。

这是我的进度:

[indent=4]
uses
    Gtk

class TestWindow:Window

    _spin:SpinButton
    _scale:Scale
    _check:CheckButton

    construct ()
        //General aspects of the window
        title = "Spin and scale"
        window_position = WindowPosition.CENTER
        destroy.connect( main_quit )

        //create the spin button
        spinAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        scaleAdjustment:Adjustment = new Adjustment( 50, 0, 100, 1, 5, 0 )
        _spin = new SpinButton( spinAdjustment, 1.0, 1 )

        //create the horizontal scale
        _scale = new Scale( Orientation.HORIZONTAL, scaleAdjustment );

        //create the check button
        _check =  new CheckButton.with_label ( "Sync both scales!" )
        _check.set_active(true)
        _check.toggled.connect( toggled )

        // organize it in a box
        var box = new Box( Orientation.VERTICAL, 0 )
        box.pack_start( _spin, true, true, 0 )
        box.pack_start( _scale, true, true, 0 )
        box.pack_start( _check, true, true, 0 )
        add( box )


    def toggled ()
        if (_check.active)
            _spin.adjustment.bind_property( "value", _scale.adjustment, "value",BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL )

init
    Gtk.init( ref args )
    var test = new TestWindow()
    test.show_all()
    Gtk.main()

有没有人建议如何 link 以仅在按下时同步的方式绑定到复选按钮?我是否应该继续尝试寻找其他解决问题的策略?

谢谢

要取消绑定,您必须调用 bind_property 方法返回的 Bindingunbind 方法:

    _binding:Binding

    construct ()
        // ...
        _check.toggled.connect( toggled )
        _check.set_active(true)
        // ...

def toggled ()
    // If there was a binding object before
    if (_binding != null)
        // .. unbind it
        _binding.unbind ()
        // and set it to null to indicate "not bound" state
        _binding = null
    // Bind if necessary
    if (_check.active)
        _binding = _spin.adjustment.bind_property( "value", _scale.adjustment, "value",BindingFlags.SYNC_CREATE | BindingFlags.BIDIRECTIONAL )

要手动执行此操作,您可以使用 value_changed 事件:

    construct ()
        // ...
        _spin.value_changed.connect (spin_value_changed)
        _scale.value_changed.connect (scale_value_changed)
        _check.toggled.connect (check_toggled)
        _check.set_active(true)
        // ...

    def spin_value_changed ()
        // Only sync, when checkbox is set
        if (_check.active)
            // Avoid circle, only set if different
            if (_scale.get_value () != _spin.get_value ())
                _scale.set_value (_spin.get_value())

    def scale_value_changed ()
        // Only sync, when checkbox is set
        if (_check.active)
            // Avoid circle, only set if different
            if (_scale.get_value () != _spin.get_value ())
                _spin.set_value (_scale.get_value())

    def check_toggled ()
        spin_value_changed ()