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

查看:184
本文介绍了从按钮命令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 .}" /> 

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

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

但是command参数只能传递当前绑定,即列表视图项.

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天全站免登陆