Home > Net >  How to implement in c # WPF MVVM checkbox checked and unchecked events
How to implement in c # WPF MVVM checkbox checked and unchecked events

Time:09-23

Xaml file code:












Could you tell me how to achieve in the viewmodel checked and unchecked events

CodePudding user response:

Since is the MVVM pattern, so the interface is just data display/operational level, in general event is used to trigger the animation in the View

Don't think about xx xx event do, which is the thinking mode of winform, under normal circumstances ViewMode should not even know the existence of the View, more do not talk about how to go to the events of the corresponding View

In the MVVM pattern, data is the center, through binding with data connection interface, and show/modify data

This data is class attribute, and respond to changes in properties, there is no doubt?

Specific to this case,
CheckBox IsChecked attributes should be bound to the ViewModel classes in a single attribute, the ViewModel through the change of the properties for reaction.

CodePudding user response:

Now I just want to in the viewmodel binding command, and can return the checkbox controls

CodePudding user response:

reference 1st floor JDD1997 response:
...
CheckBox IsChecked attributes should be bound to the ViewModel classes in a single attribute, the ViewModel response by this attribute changes.

+ 1

If use ICommand command mode, each ViewModel class to provide two commands for binding, was complicated and not too big necessary,
JDD1997 friend suggested that the properties of the binding, and the code is simpler,
 & lt; ListView Name="LV" is the Grid. The Column="0" ItemsSource="{Binding Structures}" & gt; 






 public partial class MainWindow: Window 
{
Public MainWindow ()
{
InitializeComponent();
Enclosing Structures=new List () {new My (" 1 "), the new My (" 3 "), the new My (" 5 "),};
Enclosing DataContext=this;
}

Public List Structures {get; Private set; }
Public class My
{
Public My (string id) {id=id; }
Public string Id {get; The set; }

Bool _selected;
Public bool Selected
{
The get {return _selected; }
The set {_selected=value; MessageBox. Show (Id + "check the demo:" + _selected); }
}
}
}

  •  Tags:  
  • C #
  • Related