自定义绑定控制在MvvmCross触摸项目(Custom bindable control in a

2019-08-17 04:09发布

我有它加载包含自定义按钮厦门国际银行一个MvxBaseBindableCollectionViewCell。 我希望能够通过这个自定义按钮绑定到一个视图模型。 这可能吗?

我想acheive像MyButton.ViewModel = ViewModel.ChildViewModel并有ViewModel.ChildViewModel.Name显示为按钮标题。

Answer 1:

如果你想一元定制绑定,那么就在这个教程中http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html

如果你想创建一个视图中完全绑定的UIButton那么你可以使用一些继承像这样做:

[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); }
    }
}

除了> MvvmCross V3“热金枪鱼”将包含一些辅助类,使这个简单一点的事情。



文章来源: Custom bindable control in a MvvmCross Touch project