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
}
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)
{
}
}
}
-
Answer # 1
Related articles
- c # - i want to play the animation by pressing the [unity] button
- c # - i want to be able to switch characters by pressing a button
- c # - [unity] i am changing the value of a public variable from another script, but the value seen from the script with that pub
- c# - malfunction of pressing button when using ps4 controller in unity
- unity - attack se is played by pressing the button on the result screen
- c # aspnet radio button, dropdown list
- c # - i want to enable/disable the "x" button at the top right of the xaml window
- c # - error by assets received from another person in unity
- the result of the operation when pressing the vba button and the input of the play button are different
- c # - if you create a button that transitions to the scene, make it prefab, and inherit it in each scene, 2 out of 4 will not wo
- in vuejs, after pressing the button, display a message and delete after one line
- ios - i want to add a new cell after the last cell when pressing the [tableview] button
- processing when pressing the button in the pin balloon in swift mapkit
- php - inquiry form what to do if you can't get back by pressing the back button
- python - i want to avoid "no response" when pressing gui button in pyqt
- vbnet - how to get the text of another button when you click the button
- c # - turn off the ui button
- add update process in c # save button
- javafx splitmenubutton to set selected item to button text
- dart - how to display a list when pressing a button with flutter
Related questions
- c # - i want to know the intent of mvvm "viewmodel must not know view"
- c # - i want to run storyboard from viewmodel
- c # - i want to specify the event when the enter key is pressed in the textbox in the mvvm of wpf + livet
- c# : Listen for changes in Model from ViewModel
- c # - it is displayed so that the window overlaps with webbrowser,
- [c #] i want to operate image controls collectively in wpf
- i want to automatically adjust the height of the listbox installed in the c # wpf stackpanel
- i want to make a command to delete only the selected element in c # wpf listbox
- c # - wpf listview checkbox i want to control the active state
- c # - i want to disable button when textbox is blank in reactiveproperty
There is no problem with the code presented.
After that, create a property of the class that implements ICommand and hit the left2rightCommandExecute method with it.
It is recommended to use the MVVM infrastructure instead of writing classes that implement ICommand yourself.
In the current code, there is no room for using event handlers, so you can't use code-behind, right?
If you are conscious of MVVM as you have already experienced, there will be fewer opportunities for natural and code-behind appearance.
When that happens, humanity wants to eliminate code-behind, right?
If you rarely use it, you will forget it.
You can only write (write) in the code-behind because it is completed in View. Putting it in the control library will improve maintainability and testability.
--- Add code ---
C # 7.X is like this.