1

我有一个ViewModelLocationsViewModel,其中我有一个ObservableCollection<LocationViewModel>。此外,我有一个LocationsView,它是一个MvxCollectionViewController,我在其中创建一个绑定集并将 a 绑定MvxCollectionViewSourceObservableCollection.

LocationCell, which is aMvxCollectionViewCell中,我想显示一个MonoTouch.Dialog绑定到当前选中的各种属性的a LocationViewModel。最简单的方法似乎是在 中创建一个嵌套MvxDialogViewControllerMvxCollectionViewCell但是要在 中绑定 ElementsMvxDialogViewController我显然需要创建一个绑定目标。我的问题是我真的可以将绑定目标从 传递MvxCollectionViewCellMvxDialogViewController吗?

让我也试着用一些代码来简单解释一下,以提高理解。

LocationsViewModel.cs

public class LocationsViewModel : MvxViewModel
{
    ...
    public ObservableCollection<LocationViewModel> Locations
    {
        get { return _locationDataService.Locations.Locations; }
    }
    ...
}

LocationViewModel.cs

public class LocationViewModel : MvxNotifyPropertyChanged
{
    ...
    //Tons of public properties like:
    public string Name
    {
        get { return LinkedDataModel.Name; }
        set
        {
            LinkedDataModel.Name = value;
            RaisePropertyChanged(() => Name);
        }
    }

    public double CurrentNoiseLevel
    {
        get { return LinkedDataModel.CurrentNoiseLevel; }
        set
        {
            LinkedDataModel.CurrentNoiseLevel = value;
            RaisePropertyChanged(() => CurrentNoiseLevel);
        }
    }
    ...
}

LocationsView.cs

[Register("LocationView")]
public class LocationsView
    : MvxCollectionViewController
{
    static readonly NSString LocationCellId = new NSString("LocationCell");
    private readonly bool _isInitialized;

    public LocationsView()
        : base(new UICollectionViewFlowLayout()
        {
            MinimumInteritemSpacing = 0f,
            ScrollDirection = UICollectionViewScrollDirection.Horizontal,
            MinimumLineSpacing = 0f
        })
    {
        _isInitialized = true;
        ViewDidLoad();
    }

    public new LocationsViewModel ViewModel
    {
        get { return (LocationsViewModel)base.ViewModel; }
        set { base.ViewModel = value; }
    }

    public sealed override void ViewDidLoad()
    {
        if (!_isInitialized)
            return;

        base.ViewDidLoad();

        CollectionView.RegisterClassForCell(typeof(LocationCell), LocationCellId);
        var source = new MvxCollectionViewSource(CollectionView, LocationCellId);
        CollectionView.Source = source;
        CollectionView.PagingEnabled = true;

        var set = this.CreateBindingSet<LocationsView, LocationsViewModel>();
        set.Bind(source).To(vm => vm.Locations);
        set.Apply();

        CollectionView.ReloadData();
    }
}

LocationCell.cs

public class LocationCell : MvxCollectionViewCell
{
    [Export("initWithFrame:")]
    public LocationCell(RectangleF frame)
        : base(string.Empty, frame)
    {
        InitView();
    }

    public LocationCell(IntPtr handle)
        : base(string.Empty, handle)
    {
        InitView();
    }

    private void InitView()
    {
        var cell = new LocationCellDialog();
        ContentView.Add(cell.View);
    }

    public class LocationCellDialog 
        : MvxDialogViewController
    {
        public LocationCellDialog() 
            : base(UITableViewStyle.Grouped, null, true) 
        { }

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            //How do I get the target here?
            var target = ??;
            Root = new RootElement
            {
                new Section
                {
                    new StringElement().Bind(target, t => t.Name),
                    new StringElement().Bind(target, t => t.CurrentNoiseLevel)
                }.Bind(target, t => t.Name),
            };
        }
    }
}

所以问题是我可以简单地将绑定目标从父 LocationCell 传递到嵌套的 LocationCellDialog 还是不行?

4

1 回答 1

2

MvvmCross 中的每一个bindable view都有自己的DataContext

对于顶级来说,ViewDataContextViewModel

对于 aCell中的 a ListTable或者Collection然后将DataContext设置Cell为当前显示的列表中的对象。

如果要将 a 中的任何属性数据绑定Cell到属性路径,DataContext则可以使用 Fluent 绑定语法来执行此操作。

例如,要将调用的子项的Text值绑定到列表中的子属性,您可以使用:UILabelmyLabelNamePerson

 this.CreateBinding(myLabel).For(label => label.Text).To<Person>(p => p.Name).Apply();

或者,如果您想将其绑定TextPerson您可以使用的自身:

 this.CreateBinding(myLabel).For(label => label.Text).Apply();

在你的LocationCell我认为你是说你想将DataContext嵌套LocationCellDialog的绑定到DataContext包含单元格的。

为此,您应该能够使用:

private void InitView()
{
    var cell = new LocationCellDialog();
    ContentView.Add(cell.View);
    this.CreateBinding(cell).For(cell => cell.DataContext).Apply();
}
于 2013-06-18T12:52:55.267 回答