Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add items from a listbox to a list by clicking a button without any codebehind?

Tags:

mvvm

wpf

listbox

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

<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;

    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"}
        };
    }
like image 400
Iris Classon Avatar asked Nov 13 '11 20:11

Iris Classon


1 Answers

  • first, you need to replace the Lists with ObservableCollections, so that the UI can detect when new items are added.
  • Add a SelectedItem property to your 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 Width=" 100" x:Name="Firstbox"
             ItemsSource="{Binding FoodList}"
             DisplayMemberPath="Name"
             SelectedItem="{Binding SelectedItem}" />
    
  • bind your 2nd ListBox to the NewFoodList property

  • create a command in your 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:

    <Button Margin="10" Command="{Binding AddItemCommand}" >Select</Button>
    
like image 173
Thomas Levesque Avatar answered Sep 27 '22 21:09

Thomas Levesque