如何直接从Listbox中删除(MVVM)

How to delete directly from Listbox (MVVM)

我正在尝试学习 MVVM c# 并尝试创建一个简单的程序,您可以在其中将名称添加到列表框。现在我想在按下时从列表框中删除一些项目,然后使用另一个命令按“删除我”按钮,但找不到执行此操作的方法。

视图模型是:

public class ViewModel : INotifyPropertyChanged
    {
        private Model _model;

        public ViewModel(Model model)
        {
            AddCommand = new AddNameCommand(this);
         //   DeleteCommand = new DeleteNameCommand(this);
            _model = model;
        }

        public string CurrentName
        {
            get { return _model.CurrentName; }
            set
            {
                if (value == _model.CurrentName)
                    return;
                _model.CurrentName = value;
                OnPropertyChanged();
            }
        }

       public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        
      
      
        public ObservableCollection<string> AddedNames
        {
            get { return _model.AddedNames; }
        }
        class AddNameCommand : ICommand
        {
            ViewModel parent;

            public AddNameCommand(ViewModel parent)
            {
                this.parent = parent;
                parent.PropertyChanged += delegate { CanExecuteChanged?.Invoke(this, EventArgs.Empty); };
            }

            public event EventHandler CanExecuteChanged;

            public bool CanExecute(object parameter) { return !string.IsNullOrEmpty(parent.CurrentName); }

            public void Execute(object parameter)
            {
                parent.AddedNames.Add(parent.CurrentName);
                parent.CurrentName = null;
            }
        }

        class DeleteNameCommand : ICommand
        {
            
            public void Execute(object parameter)
            {
                parent.AddedNames.Remove(...?);

            }
        }


        public ICommand AddCommand { get; private set; }
    }
}

视图是

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="150"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <DockPanel>
            <TextBlock Text="Added Names"  
        DockPanel.Dock="Top" Margin="5,3"/>
            <ListBox ItemsSource="{Binding AddedNames}"></ListBox>
        </DockPanel>

        <GridSplitter Grid.Column="1"  
    VerticalAlignment="Stretch" Width="5"  
     Background="Gray" HorizontalAlignment="Left" />

        <Grid Grid.Column="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Text="Name" Margin="5,3"/>
            <TextBox Grid.Row="0" Grid.Column="1"   Text="{Binding CurrentName, UpdateSourceTrigger=PropertyChanged}" Margin="5,3"/>

            <TextBlock Grid.Row="1" Text="Your name is:" Margin="5,3"/>
            <TextBlock Grid.Row="1" Grid.Column="1"  Text="{Binding CurrentName}"  Margin="5,3"/>


            <Button Grid.Row="2" Grid.ColumnSpan="2" HorizontalAlignment="Left"  
         Content="Add Me" Command="{Binding AddCommand}" Margin="5,3" MinWidth="75" />
            <Button Grid.Row="2" HorizontalAlignment="Left"  
         Content="Delete Me" Command="{Binding DeleteCommand}" Margin="5,29,0,-23" MinWidth="75" />

        </Grid>

    </Grid>
</Window>

模型是

public class Model
    {
        public string CurrentName { get; set; }
       

        public ObservableCollection<string> AddedNames { get; } = new ObservableCollection<string>();
    }

要么有一个 SelectedName 属性

<ListBox ItemsSource="{Binding AddedNames}" SelectedItem="{Binding SelectedName}"/>

或者我喜欢使用的是删除行

<ItemsControl ItemsSource="{Binding AddedNames}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <DockPanel>
                <Button DockPanel.Dock="Right" Content="X" Command="{Binding DataContext, RelativeSource={RelativeSource AncestorType=ItemsControl}}" CommandParameter="{Binding}"/>
                <TextBlock Text="{Binding}"/>
            </DockPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

public void Execute(object parameter)
{
    parent.AddedNames.Remove((string)parameter);
}

添加一个SelectedName 属性到视图模型:

public string SelectedName { get; set; }

将视图中的 ListBox 绑定到它:

<ListBox ItemsSource="{Binding AddedNames}" SelectedItem="{Binding SelectedName}" />

然后只需在命令中使用 属性 的值:

public void Execute(object parameter)
{
   parent.AddedNames.Remove(parent.SelectedName);
}