【问题标题】:Xamarin.Forms How to two-way bind to a control in ListviewXamarin.Forms 如何双向绑定到 Listview 中的控件
【发布时间】:2018-05-22 12:14:48
【问题描述】:

我很难弄清楚如何为列表视图中的控件设置双向绑定。

我正在使用 ReactiveUI 和 Xamarin.Forms。

在这种情况下,我想加载具有数量的对象列表。这是在页面加载时最初设置的。但是,我希望能够在程序运行时更改视图中的这些数量值。我为此使用了一个条目。

无法为列表本身设置双向绑定(在代码后面完成,反应方式)是不可能的。它会出错。

是否有其他方法可以观察对 Entry 控件中的 Text 属性所做的更改并将它们反映到我的视图模型中列表中的相应项目?

我一直无法找到解决方案,并且真的不知道如何解决这个问题。

这是我的 XAML 代码:

<CustomControls:AutoLoadListView.ItemTemplate>
       <DataTemplate>
               <ViewCell>
                        <StackLayout Margin="20,0,0,0" Orientation="Vertical" HorizontalOptions="StartAndExpand">
                            <Label Margin="0,5,0,-5" Style="{StaticResource ViewCellPrimaryLabelStyle}"   x:Name="txt" Text="{Binding itemname}" />
                            <Label Margin="0,-5,0,5" Style="{StaticResource ViewCellSecondaryLabelStyle}" x:Name="barcode" Text="{Binding productcode}" />
                        </StackLayout>
                        <Entry Margin="5,0,5,0" x:Name="quantity" Text="{Binding quantity}">
                             <Entry.BindingContext>
                                 <ViewModel:AankoopEditViewModel />
                             </Entry.BindingContext>
                        </Entry>
                        <Image Margin="5,5,5,5" x:Name="delete"  Source="{Mobile:ImageResource tbin_pos.png}">
                             <Image.GestureRecognizers>
                                 <TapGestureRecognizer
                                  Command="{Binding Path=BindingContext.DeleteCommand,Source={x:Reference Name=AankoopEditPage}}"
                                  CommandParameter="{Binding}" />
                             </Image.GestureRecognizers>
                        </Image>
                </ViewCell>
        </DataTemplate>
</CustomControls:AutoLoadListView.ItemTemplate>

我的视图模型:

public class AankoopEditViewModel : BaseViewModel
{

    private VmPurchase Purchase;

    public AankoopEditViewModel()
    {

        PurchaseList = new ReactiveObservableCollection<AankoopEditListItem>()
        {
            ChangeTrackingEnabled = true
        };


        this.WhenAnyValue(x => x.PurchaseID).SubscribeOn(RxApp.MainThreadScheduler).Subscribe((x) =>
        {
            this.Purchase = DatabaseHelper.Purchase.LoadSingleById<VmPurchase>(PurchaseID);

            if (Purchase != null)
            {
                this.Title = Purchase.supplier.name;

                using (PurchaseList.SuppressChangeNotifications())
                {
                    foreach (var detail in Purchase.purchasedetails)
                    {                          
                        PurchaseList.Add(new AankoopEditListItem { productcode = detail.item.code, itemname = detail.item.namenl, identifier = detail.key, quantity = detail.quantity.ToString() });

                    }
                }
            }
        });

        try
        {
            this.WhenAnyValue(x => x.PurchaseList).SubscribeOn(RxApp.MainThreadScheduler).Subscribe((x) =>
            {
                Console.WriteLine("The List has changed");
            });
        }
        catch (Exception e)
        {
            return;
        }
    }




    private string _purchaseID;

    public string PurchaseID
    {
        get { return _purchaseID; }
        set { this.RaiseAndSetIfChanged(ref _purchaseID, value); }
    }

    private ReactiveObservableCollection<AankoopEditListItem> _purchases;

    public ReactiveObservableCollection<AankoopEditListItem> PurchaseList
    {
        get
        {
            return this._purchases;
        }
        set
        {
            this.RaiseAndSetIfChanged(ref _purchases, value);
        }
    }

我的模特:

public class AankoopEditListItem : ReactiveObject
{
    public string identifier { get; set; }
    public string itemname { get; set; }
    public string productcode { get; set; }
    public string quantity { get; set; }
}

【问题讨论】:

    标签: c# xaml listview binding xamarin.forms


    【解决方案1】:

    当你这样做时要小心

    <Entry.BindingContext>
       <ViewModel:AankoopEditViewModel />
    </Entry.BindingContext>
    

    您为每个项目创建视图模型的新实例,并将Entry 绑定到它。如果您想将条目绑定到行视图模型,只需将其删除并保持绑定原样 (Text="{Binding quantity}")

    【讨论】:

    • 我现在尝试将基础对象作为资源传递给条目。所以我可以使用它的标识符来更新它。
    • 不,不是真的。我为此找到了一个很大的解决方法。它使用事件并使用其发送者对象来获取父视单元。比我通过 id 获得属性,并从那里获得相应的值。比调用命令......所以是的。它很乱。
    猜你喜欢
    • 2014-10-03
    • 1970-01-01
    • 2011-08-27
    • 2011-07-06
    • 2021-04-20
    • 2017-02-16
    • 2019-06-06
    • 2010-12-23
    • 2018-08-05
    相关资源
    最近更新 更多