2

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

我正在尝试实现类似MyButton.ViewModel = ViewModel.ChildViewModel并将 ViewModel.ChildViewModel.Name 显示为按钮标题。

4

1 回答 1

2

如果你想自定义绑定一个单元格,那么在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); }
    }
}

Aside> MvvmCross v3 "Hot Tuna" 将包含一些帮助类,以使其更简单一些。

于 2013-02-25T18:01:47.430 回答