Home>

[Command creation]
1) Button press
2) Set Item selected in list box 1 to Selected = false
* Currently, List box 1 displays Selected = true

private void left2rightCommandExecute (object parameter)
{
I will write processing here
}

What else do I want to ask

In MVVM, instead of writing an event handler in the code-behind, is it okay to write a command to the ViewModel as above and bind it with the xaml of the view? (= Do not write anything other than InitializeComponent () in code-behind)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using TestDialog.Common;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Data;

namespace Test.ViewModels
{
    class MainViewModel: ViewModelBase
    {
        private ObservableCollection<ItemViewModel>_Items;
        public ObservableCollection<ItemViewModel>Items
        {
            get
            {
                return _Items;
            }
            set
            {
                SetProperty (ref _Items, value);
            }
        }
        public ICollectionView LeftView {get;}
        public ICollectionView RightView {get;}
        public MainViewModel ()
        {
            Items = new ObservableCollection<ItemViewModel>();
            Items.Add (new ItemViewModel () {Name = "C1", Selected = true});
            Items.Add (new ItemViewModel () {Name = "C2", Selected = false});
            Items.Add (new ItemViewModel () {Name = "C3", Selected = true});
            LeftView = new CollectionViewSource {Source = Items} .View;
            LeftView.Filter = x =>(x as ItemViewModel) .Selected == true;
            RightView = new CollectionViewSource {Source = Items} .View;
            RightView.Filter = x =>(x as ItemViewModel) .Selected == false;
        }

        public class ItemViewModel: ViewModelBase
        {
            private bool _Selected;
            public bool Selected
            {
                get {return _Selected;}
                set
                {
                    SetProperty (ref _Selected, value);
                }
            }
            private string _Name;
            public string Name
            {
                get {return _Name;}
                set
                {
                    SetProperty (ref _Name, value);
                }
            }
        }
        private void left2rightCommandExecute (object parameter)
        {
        }

    }
}