【问题标题】:Mvvmcross xamarin forms button command inside Listview templateMvvmcross xamarin 在 Listview 模板中形成按钮命令
【发布时间】:2021-09-03 05:38:43
【问题描述】:

我有 viewmodel 和 view,它在 ListView 中列出了我的 Model 对象,每个列表条目都包含 2 个按钮:详细信息和预订。

查看


    <ListView
            ItemsSource="{Binding Models}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Button Command="{Binding Delete}"></Button>
                    <Button Command="{Binding Details}"></Button>
                </StackLayout>
              
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

视图模型

   class ModelsViewModel : MvxViewModel
    {
        private ObservableCollection<Model> models;
        public ObservableCollection<Model> Models
        {
            get
            {
                return models;
            }
            set
            {
                SetProperty(ref models, value);
            }
        }

        private IMvxCommand details;
        public IMvxCommand Details
        {
            get
            {
                details = details ?? new MvxCommand<ListItem>(o =>
                {

                });
                return details;
            }
        }

        private IMvxCommand delete;
        public IMvxCommand Delete
        {
            get
            {
                delete = delete ?? new MvxCommand<ListItem>(o =>
                {

                });
                return delete;
            }
        }
    }

每个按钮都应该在 ModelsViewModel 的 Details/Delete 方法中调用 lambda。

发生的情况是列表视图中的数据绑定指向不存在此类方法的模型对象。我的问题是,有没有办法改变列表视图中项目的绑定上下文,并将点击的项目作为参数传递?

我尝试弄乱 Source 并设置 BindingContext 没有成功

【问题讨论】:

    标签: c# xamarin xamarin.forms mvvmcross


    【解决方案1】:

    试试这样:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Name="parentContePage">
    
        <ListView ItemsSource="{Binding Models}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackLayout>
                        <Button Command="{Binding Source={x:Reference parentContePage}, Path=BindingContext.Delete}"
                        CommandParameter="{Binding .}"/>
                        <Button Command="{Binding Source={x:Reference parentContePage}, Path=BindingContext.Details}"
                        CommandParameter="{Binding .}"/>
                    </StackLayout>                
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </ContentPage>
    

    确保将x:Name 提供给您的父内容页面,如上所述。

    【讨论】:

    • 不幸的是它没有解决问题。单击按钮时没有任何反应。我的问题是 Mvvmcross 可能不支持该功能,或者我还没有找到解决方案。
    【解决方案2】:

    首先,我可以发表评论,所以我会发布一个答案。 尝试将命令从MvxCommand&lt;ListItem&gt;更改为MvxCommand&lt;Model&gt;,因为命令参数在遵循上一个答案后采用了当前模型。

    【讨论】:

      猜你喜欢
      • 2019-07-30
      • 1970-01-01
      • 2017-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-01-16
      • 1970-01-01
      • 2019-05-11
      • 2020-04-02
      相关资源
      最近更新 更多