0

Currently binding to "Visibility" sets Hidden=true. How would you create a generic Visibility binding which also changes a constraint: sets the view height to 0 ?

Softlion
  • 12,281
  • 11
  • 58
  • 88

1 Answers1

1

For a tutorial on creating bindings, see the N=28 video on http://mvvmcross.blogspot.com/

To replace the existing visibility binding, simply create your own class based on https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUIViewVisibleTargetBinding.cs

public class MyUIViewVisibleTargetBinding : MvxBaseUIViewVisibleTargetBinding
{
    public MyUIViewVisibleTargetBinding(UIView target)
        : base(target)
    {
    }

    protected override void SetValueImpl(object target, object value)
    {
        var view = View;
        if (view == null)
            return;

        var visible = value.ConvertToBoolean();
        // your code here
        // - in place of or in addition to:
        // view.Hidden = !visible;
    }
}

And register this as the last step in Setup using:

protected override void InitializeLastChance()
{
     base.InitializeLastChance();

     var registry = Mvx.Resolve<IMvxTargetBindingFactoryRegistry>();
     registry.RegisterCustomBindingFactory<UIView>("Visible",
                                                    view =>
                                                    new MyUIViewVisibleTargetBinding(view));

}

For more on replacing existing bindings, see MVVMCross Binding decimal to UITextField removes decimal point


Note that if you want to replace all Visible bindings, then you might want to replace all of Visible, Visibility and Hidden - see registrations in https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/MvxTouchBindingBuilder.cs#L42

Community
  • 1
  • 1
Stuart
  • 66,722
  • 7
  • 114
  • 165
  • well i know how to create bindings, just would like to find a generic way of enabling/disabling some constraints : the ones which sets the view height. A generic way. Maybe by enumerating all constraints from subviews which link to the container view + any height constraints on the container view itself, disabling them, and setting a new one which forces a 0 height. But reenabling constraints would need to know which one were disabled. – Softlion Mar 25 '14 at 14:00
  • I think a longer question might - it might help answerers to work out what you are asking. Maybe try editing your question to give a full description - preferably with code - of what you have setup and what you want the binding to change. For further inspiration, JS has some good advice on asking questions on https://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Stuart Mar 25 '14 at 15:12
  • Well sorry if we didn't understand each other Stuart. You don't need to tackle me. I should not have put mvvmcross in this question. – Softlion Mar 25 '14 at 18:26