首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何直接从Listbox (MVVM)中删除

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

Stack Overflow用户
提问于 2022-02-02 13:07:35
回答 2查看 126关注 0票数 1

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

视图模型是:

代码语言:javascript
运行
复制
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; }
    }
}

视图是

代码语言:javascript
运行
复制
<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>

模型是

代码语言:javascript
运行
复制
public class Model
    {
        public string CurrentName { get; set; }
       

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

Stack Overflow用户

发布于 2022-02-02 13:18:31

要么具有SelectedName属性

代码语言:javascript
运行
复制
<ListBox ItemsSource="{Binding AddedNames}" SelectedItem="{Binding SelectedName}"/>

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

代码语言:javascript
运行
复制
<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>

代码语言:javascript
运行
复制
public void Execute(object parameter)
{
    parent.AddedNames.Remove((string)parameter);
}
票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70956193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档