【问题标题】:Get ListView from Button Command xamarin.forms MVVM从按钮命令 xamarin.forms MVVM 获取 ListView
【发布时间】:2018-06-02 00:43:15
【问题描述】:

我在使用 MVVM 模式的 xamarin.forms 应用程序中的 ListView 存在问题。我希望你能帮助我。 这是我的 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:

 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 中删除它

感谢您的帮助

【问题讨论】:

  • x:Name 的使用不是 MVVM 模式,请尝试摆脱这种情况。删除命令在您的列表项的视图模型上,我正确吗?当您使用视图模型创建列表时,将列表作为对视图模型的引用传递。然后您可以在项目视图模型中轻松删除它。

标签: listview xamarin mvvm xamarin.forms command


【解决方案1】:

首先更改删除按钮 XAML。

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

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

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

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

您可以直接删除该项目。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 2021-01-28
    • 2011-04-25
    • 2015-06-07
    相关资源
    最近更新 更多