1

ViewModel呼ばれるLocationsViewModelがあり、その中に がありObservableCollection<LocationViewModel>ます。さらに、バインディング セットを作成し、 を にバインドするLocationsViewがあります。MvxCollectionViewControllerMvxCollectionViewSourceObservableCollection

LocationCellであるで、現在選択されている のさまざまなプロパティにバインドされているMvxCollectionViewCellを表示したいと考えています。でネストされたを作成するのが最も簡単な方法のようですが、 で をバインドする には、明らかに Binding Target を作成する必要があります。私の質問は、バインディング ターゲットを から に渡すことができるかどうかです。MonoTouch.DialogLocationViewModelMvxDialogViewControllerMvxCollectionViewCellElementsMvxDialogViewControllerMvxCollectionViewCellMvxDialogViewController

また、理解を深めるために、いくつかのコードを使用して簡単に説明してみましょう。

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

トップレベルの場合、ViewこれDataContextViewModel

Cell内の のList場合、Tableが現在表示されCollectionDataContextいるリスト内のオブジェクトに設定されます。Cell

Cell内の任意のプロパティを のプロパティ パスにデータ バインドする場合DataContextは、Fluent バインディング構文を使用して実行できます。

たとえば、呼び出されたText子の値をリスト内の子プロパティにバインドするには、次を使用できます。UILabelmyLabelNamePerson

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

Textまたは、をそれ自体にバインドしたい場合は、Person次を使用できます。

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

あなたは、ネストされたセルを含むセルLocationCellにバインドしたいと言っていると思います。DataContextLocationCellDialogDataContext

これを行うには、次を使用できる必要があります。

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 に答える