Custom bindable control in a MvvmCross Touch project

后端 未结 1 1270
遥遥无期
遥遥无期 2021-01-21 23:19

I have a MvxBaseBindableCollectionViewCell which loads a xib that contains a custom button. I would like to be able to pass this custom button a ViewModel to bind to. Is this

相关标签:
1条回答
  • 2021-01-21 23:44

    If you want to custom bind a cell, then there's a tutorial on this in http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

    If you want to create a fully bindable UIButton within that View then you can do this using some inheritance like:

    [Register("MyButton")]
    public class MyButton
        : UIButton
          , IMvxServiceConsumer
    {
        private IList<IMvxUpdateableBinding> _bindings;
    
        private const string BindingText = "SpecialTitle Customer.Name";
    
        public MyButton()
        {
        }
    
        public MyButton(IntPtr handle)
            : base(handle)
        {
        }
    
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                foreach (var binding in _bindings)
                {
                    binding.Dispose();
                }
                _bindings.Clear();
            }
            base.Dispose(disposing);
        }
    
        private object _dc;
    
        public object DataContext
        {
            get { return _dc; }
            set
            {
                _dc = value;
                if (_bindings == null)
                {
                    var binder = this.GetService<IMvxBinder>();
                    _bindings = binder.Bind(_dc, this, BindingText).ToList();
                }
                else
                {
                    foreach (var binding in _bindings)
                    {
                        binding.DataContext = _dc;
                    }
                }
            }
        }
    
        public string SpecialTitle
        {
            get { return this.GetTitle(UIControlState.Normal); }
            set { this.SetTitle(value, UIControlState.Normal); }
        }
    }
    

    Aside> MvvmCross v3 "Hot Tuna" will contain some helper classes to make this a bit simpler to do.

    0 讨论(0)
提交回复
热议问题