【发布时间】: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