Custom bindable control in a MvvmCross Touch project

做~自己de王妃 提交于 2020-01-11 10:27:12

问题


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 possible?

I'm trying to acheive something like MyButton.ViewModel = ViewModel.ChildViewModel and have ViewModel.ChildViewModel.Name show as the button title.


回答1:


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.



来源:https://stackoverflow.com/questions/15073262/custom-bindable-control-in-a-mvvmcross-touch-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!