MvvmCross Touch项目中的自定义可绑定控件 [英] Custom bindable control in a MvvmCross Touch project

查看:58
本文介绍了MvvmCross Touch项目中的自定义可绑定控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MvxBaseBindableCollectionViewCell,它加载包含自定义按钮的xib.我希望能够将此自定义按钮传递给ViewModel进行绑定.这可能吗?

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?

我正在尝试实现MyButton.ViewModel = ViewModel.ChildViewModel之类的功能,并将ViewModel.ChildViewModel.Name显示为按钮标题.

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

推荐答案

如果您要自定义绑定单元格,那么

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

如果您想在该视图中创建一个完全可绑定的UIButton,则可以使用一些继承来做到这一点,例如:

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热金枪鱼"将包含一些帮助程序类,以使此操作更简单.


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

这篇关于MvvmCross Touch项目中的自定义可绑定控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆