如何通过单击没有任何代码隐藏的按钮将列表框中的项目添加到列表中?

时间:2021-11-28 20:32:39

I am new to MVVM, and also fairly new to WPF. As a matter of fact I started programming just a few months ago. MVVM is really dng my head in with the binding concept, and I have been trying for days now to just simply make an application that allows you to select an item from a listbx, and when you click on the add button the selected item should be saved in a new list. The second listbox displays the latest items added, and you can select an item and delete it by using another button. ususally I would go for the click event and decorate my codebehind with pretty little methods, but I really want to learn how to do all this by using bindings and no codebehind. I would be extremly happy for any help, and please remember that I am new to this and I really want to keep it as simple as possible :) with kind regards Daniela

我是MVVM的新手,也是WPF的新手。事实上,我几个月前开始编程。 MVVM实际上是关于绑定概念的,我已经尝试了几天只是简单地创建一个允许您从listbx中选择项目的应用程序,当您单击添加按钮时,所选项目应该是保存在新列表中。第二个列表框显示添加的最新项目,您可以选择一个项目并使用另一个按钮将其删除。通常我会去点击事件并用很少的方法装饰我的代码隐藏,但我真的想通过使用绑定和没有代码隐藏来学习如何做到这一切。我会非常高兴得到任何帮助,请记住我是新手,我真的希望尽可能简单:)与亲切的问候Daniela

<WrapPanel HorizontalAlignment="Center" Margin=" 10">
   <ListBox x:Name="Firstbox" 
            Width="100"
            ItemsSource="{Binding FoodList}"
            DisplayMemberPath="Name" >
   </ListBox>
   <Button Margin="10 >Select</Button>
   <ListBox Width="100"></ListBox>

private List _foodList;

private List _foodList;

    public List<FoodItem> FoodList
    {
        get { return _foodList; }
        set { _foodList = value; }
    }

    private List<FoodItem> _newFoodList;

    public List<FoodItem> NewFoodList
    {
        get { return _newFoodList; }
        set { _newFoodList = value; }
    }

    public MainViewModel()
    {
        InitializeCommands();
        GetFood();
    }
    private void GetFood()
    {
        FoodList = new List<FoodItem>()
        {
            new FoodItem() {Name="Applepie"}, 
            new FoodItem() {Name="Scones"}
        };
    }

1 个解决方案

#1


7  

  • first, you need to replace the Lists with ObservableCollections, so that the UI can detect when new items are added.
  • 首先,您需要使用ObservableCollections替换Lists,以便UI可以检测何时添加新项目。
  • Add a SelectedItem property to your ViewModel:

    将SelectedItem属性添加到ViewModel:

    private FoodItem _selectedItem;
    public FoodItem SelectedItem
    {
        get { return _selectedItem;}
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }
    
  • bind the SelectedItem property of the 1st ListBox to this property:

    将第一个ListBox的SelectedItem属性绑定到此属性:

    <ListBox Width=" 100" x:Name="Firstbox"
             ItemsSource="{Binding FoodList}"
             DisplayMemberPath="Name"
             SelectedItem="{Binding SelectedItem}" />
    
  • bind your 2nd ListBox to the NewFoodList property

    将您的第二个ListBox绑定到NewFoodList属性

  • create a command in your ViewModel:

    在ViewModel中创建一个命令:

    private DelegateCommand _addItemCommand;
    public ICommand AddItemCommand
    {
        get
        {
            if (_addItemCommand == null)
            {
                _addItemCommand = new DelegateCommand(AddItem);
            }
            return _addItemCommand;
        }
    }
    
    void AddItem()
    {
        if (SelectedItem != null)
            NewFoodList.Add(SelectedItem);
    }
    
  • And finally, bind the button's Command property to the AddItemCommand property:

    最后,将按钮的Command属性绑定到AddItemCommand属性:

    <Button Margin="10" Command="{Binding AddItemCommand}" >Select</Button>
    

#1


7  

  • first, you need to replace the Lists with ObservableCollections, so that the UI can detect when new items are added.
  • 首先,您需要使用ObservableCollections替换Lists,以便UI可以检测何时添加新项目。
  • Add a SelectedItem property to your ViewModel:

    将SelectedItem属性添加到ViewModel:

    private FoodItem _selectedItem;
    public FoodItem SelectedItem
    {
        get { return _selectedItem;}
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }
    
  • bind the SelectedItem property of the 1st ListBox to this property:

    将第一个ListBox的SelectedItem属性绑定到此属性:

    <ListBox Width=" 100" x:Name="Firstbox"
             ItemsSource="{Binding FoodList}"
             DisplayMemberPath="Name"
             SelectedItem="{Binding SelectedItem}" />
    
  • bind your 2nd ListBox to the NewFoodList property

    将您的第二个ListBox绑定到NewFoodList属性

  • create a command in your ViewModel:

    在ViewModel中创建一个命令:

    private DelegateCommand _addItemCommand;
    public ICommand AddItemCommand
    {
        get
        {
            if (_addItemCommand == null)
            {
                _addItemCommand = new DelegateCommand(AddItem);
            }
            return _addItemCommand;
        }
    }
    
    void AddItem()
    {
        if (SelectedItem != null)
            NewFoodList.Add(SelectedItem);
    }
    
  • And finally, bind the button's Command property to the AddItemCommand property:

    最后,将按钮的Command属性绑定到AddItemCommand属性:

    <Button Margin="10" Command="{Binding AddItemCommand}" >Select</Button>