使用Viewmodel删除和编辑列表视图中的项目,而不使用代码隐藏

问题描述 投票:0回答:3

在显示的代码中,我需要知道要替换的代码而不是代码中的问号。我需要删除,编辑和更新列表视图中的项目,而无需在代码后面编写任何代码。我只想通过Icommand通过绑定视图和视图模型来执行这些操作

这是我的模型Playlist.cs中的一个类

namespace MvvmDemo.Models {public class Playlist {public string Title {get;组; }}}

这是我的viewmodel PlaylistsViewModel.cs中的一个类

namespace MvvmDemo.ViewModels {public class PlaylistsViewModel {public ObservableCollection Playlists {get;私人集; } = new ObservableCollection();

    public ICommand AddPlaylistCommand { get; private set; }

    public ICommand DeletePlaylistCommand { get; private set; }

     public ICommand EditPlaylistCommand { get; private set; }

    public PlaylistsViewModel()
    {
        AddPlaylistCommand = new Command(AddPlaylist);
        DeletePlaylistCommand = new Command(DeletePlaylist);
    }

    public void AddPlaylist()
    { 
        var newPlaylist = "Playlist " + (Playlists.Count + 1);
        Playlists.Add(new Playlist { Title = newPlaylist });
    }

    public void DeletePlaylist()
    {
        ????????????????
    }
    public void EditPlaylist()
    {
        ????????????????
    }
}

}

c# xamarin
3个回答
0
投票

你必须使命令参数化并通过参数传递绑定数据。从该数据中,您可以获取selected的索引值。使用该值从列表中删除该项。

Playlists.RemoveAt( “INDEX_NUMBER”);

要在视图中更新它,请使用“INotify属性”


0
投票

如果要在ListView中删除和编辑项目,首先,您需要使用ICommand,然后您可能需要使用INotifyPropertyChanged来实现Inotify。我做了一个你可以看看的样本。选择一个项目并用鼠标左键长按,您将看到两种方法,删除项目和编辑项目操作。

 <ContentPage.Content>
    <StackLayout>
        <ListView
            x:Name="mylistview"
            ItemsSource="{Binding lists}"
            SelectedItem="{Binding selecteditem}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <ViewCell.ContextActions>
                            <MenuItem
                                Command="{Binding BindingContext.DeletePlaylistCommand, Source={x:Reference Name=mylistview}}"
                                IsDestructive="true"
                                Text="Delete Item" />
                            <MenuItem
                                Command="{Binding BindingContext.EditPlaylistCommand, Source={x:Reference Name=mylistview}}"
                                IsDestructive="true"
                                Text="Edit Item" />
                        </ViewCell.ContextActions>
                        <StackLayout Padding="15,0">
                            <Label Text="{Binding Title}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>


[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page19 : ContentPage, INotifyPropertyChanged
{
    public ObservableCollection<Playlist> lists { get; set; }
    //public RelayCommand1 AddPlaylistCommand { get; set; }
    public RelayCommand DeletePlaylistCommand { get; set; }
    public RelayCommand EditPlaylistCommand { get; set; }

    private Playlist _selecteditem;
    public Playlist selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            RaisePropertyChanged("selecteditem");

        }
    }

    public Page19 ()
    {
        InitializeComponent ();
        lists = new ObservableCollection<Playlist>()
        {
            new Playlist(){Id=1,Title="list 1"},
             new Playlist(){Id=2, Title="list 2"},
              new Playlist(){Id=3,Title="list 3"},
               new Playlist(){Id=4,Title="list 4"},
                new Playlist(){Id=5,Title="list 5"},
                new Playlist(){Id=6,Title="list 6"},
        };

        DeletePlaylistCommand = new RelayCommand(DeletePlaylist);
        EditPlaylistCommand = new RelayCommand(EditPlaylist);
        selecteditem = lists[0];
        this.BindingContext = this;
    }

    public void AddPlaylist()
    {

    }

    public void DeletePlaylist()
    {
        Playlist item = selecteditem;
        lists.Remove(item);

    }

    public void EditPlaylist()
    {
        Playlist item = selecteditem;
        int id = item.Id;
        foreach(Playlist playl in lists.Where(a=>a.Id==id))
        {
            playl.Title = "chenge title";
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class Playlist: INotifyPropertyChanged
{
    private int _Id;
    public int Id
    {
        get { return _Id; }
        set
        {
            _Id = value;
            RaisePropertyChanged("Id");
        }
    }
    private string _Title;
    public string Title
    {
        get { return _Title;}
        set
        {
            _Title = value;
            RaisePropertyChanged("Title");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

这是RelayCommd:

 public class RelayCommand : ICommand
{
    readonly Action _execute;

    public RelayCommand(Action execute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
    }

    public event EventHandler CanExecuteChanged;

    public bool CanExecute(object parameter)
    {
        return true;
    }
    public void Execute(object parameter)
    {
        _execute();
    }

}

enter image description here


0
投票

您可以使用observablecollection。它将反映项目的添加,删除操作到列表视图。对于编辑项目,您必须为您正在编辑的所有属性更改属性。要简化该属性更改,您可以将属性更改事件实现到您的播放列表模型类。

喜欢

public void DeletePlaylist()
    {
        Playlists.Remove(newPlaylist);
    }
    public void EditPlaylist()
    {
        newPlaylist.Title="Refreshed Playlist"
    }

public class Playlist:INotifyPropertyChanged
{
    private string title;
    public string Title
    {
       get{return title;}
       set{title=value;
           NotifyPropertyChanged();}  
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.