从按钮命令 xamarin.forms MVVM 获取 ListView [英] Get ListView from Button Command xamarin.forms MVVM

查看:39
本文介绍了从按钮命令 xamarin.forms MVVM 获取 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 MVVM 模式的 xamarin.forms 应用程序中的 ListView 上有问题.我希望你能帮助我.这是我的 xaml:

I have an issue on my ListView in my xamarin.forms application where I use MVVM pattern. I hope you could help me. Here is my xaml:

 <ListView x:Name="MissingVolumeListView"  
                          ItemsSource="{Binding Volumes}"
                          SelectedItem ="{Binding Id}"
                             >
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <ViewCell>
                                        <Grid x:Name="Item">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="40"/>
                                            </Grid.ColumnDefinitions>
                                            <Label  Text="{Binding Name}" Style="{StaticResource UsualLabelTemplate}"/>
                                            <Button Grid.Column="1" x:Name="DeleteButton" Text ="-" BindingContext="{Binding Source={x:Reference MissingVolumeListView}, Path=BindingContext}"   Command="{Binding DeleteVolumeCommand}" CommandParameter="{Binding Source={x:Reference Item}, Path=BindingContext}"/>
                                        </Grid>
                                    </ViewCell>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>

然后我在 ViewModel 中获得 DeleteVolumeCommand:

Then I get the DeleteVolumeCommand in my ViewModel:

 private ICommand _deleteVolumeCommand;
    public ICommand DeleteVolumeCommand
    {
        get
        {
            {
                if (_deleteVolumeCommand == null)
                {
                    _deleteVolumeCommand = new Command((e) =>
                    {
                        var item = (e as Volume);
                        int index = MissingVolumeListView.Items.IndexOf(item);                       
                    });
                }

                return _deleteVolumeCommand;
            }

        }
    }

如您所见,我想要的是在我单击 ListView 中的按钮时获得选定的项目.然后,我想让我的 ListView 获取所选项目的索引以从我的 ListView 中删除它

As you can see, what I want is to get selected item when I click a Button in my ListView. Then, I want to get my ListView to get index of the selected item to delete it from my ListView

感谢您的帮助

推荐答案

首先改变你的删除按钮 XAML.

First of all change your delete button XAML.

<Button Grid.Column="1" 
        Text ="-" 
        Command="{Binding Path=BindingContext.DeleteVolumeCommand, Source={x:Reference MissingVolumeListView}}" 
        CommandParameter="{Binding .}" /> 

该命令需要通过列表视图名称将其绑定上下文更改为视图模型.

The command needs to have its binding context changed to the view model via the listview name.

但是,命令参数可以只传递当前绑定,即列表视图项.

The command parameter however can just pass the current binding which is the list view item.

在您的视图模型中,您不能通过名称引用任何控件.而是使用列表视图将其 ItemsSource 绑定到的列表 - Volumes.

In your view model you cannot reference any controls via name. Instead use the list that the list view has its ItemsSource bound to - Volumes.

您可以直接删除该项目.

You can remove the item directly.

_deleteVolumeCommand = new Command((e) =>
{
    var item = (e as Volume);
    Volumes.Remove(item);                       
});

如果 Volumes 不是 ObservableCollection,请记住还要调用 notify 属性已更改.

Remember to also call notify property changed if Volumes is not an ObservableCollection.

这篇关于从按钮命令 xamarin.forms MVVM 获取 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆