c# - using a button to make a user control visible in WPF using MVVM pattern -


I have a grid in the WPF that contains a button for which user controls should be visible. How can I use the MVVM pattern and / or the back codes?

How to get a full example on it with the example of ICommand interface in MCVM, Your main should look like this

XAML:

  & lt; Window x: Class = "WpfApplication3.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/ Xaml "title =" main window "height =" 350 "width =" 525 "xmlns: my =" clr-namespace: wpfApplication3 "& gt; & Lt; Grid & gt; & Lt; My: UserControll background = "Aqua" visibility = "{Binding changes Kantrolwaibiliti mode = Dove, Apdetsorstrigr = property change}" horizontal Elaiment = "Left" Margin = "111,66,0,0" x: Name = "UserControlll" Vertical alignment = "top" height = "156" width = "195" /> & Lt; Button Content = "button" height = "36" horizontal alignment = "Left" Margin = "36,18,0,0" name = "button 1" Vrtiklelinement = "top" width = "53" Command = "{Binding Maybtnklik Commons} "/> & lt; / Grid & gt; & Lt; / Window & gt;  

MainWindow.cs

 using the  system; Using System.ComponentModel; Using System.Windows; Using System.Windows.Input; Namespace WpfApplication3 {/// & lt; Summary & gt; /// MainWindow.xaml for Interaction Logic /// & lt; / Summary & gt; Public Partial Class Main Window: Window {Public Main Window () {Initial Group (); This.DataContext = New MyViewModel (); }}}  

ViewModel:

  public class MyViewModel: INotifyPropertyChanged {public MyViewModel () {_myCommand = new MyCommand (FuncToCall , FuncToEvaluate); } Private ICommand _myCommand; Get public icmand mybutanclick {back {_myCommand; } Set {_myCommand = value; }} Private is zero FuncToCall (object context) click the button {// This is called // For example, if (this.ChangeControlVisibility == visibility. Collapsed) {this.ChangeControlVisibility = Visibility.Visible; } And {this.ChangeControlVisibility = Visibility. Collapsed; }} Private Bull Functivat (Object Context) {return true; } Private Visibility _view = Visibility. Visible; Change public visibilitygroupsinclude {return {return_view; } Set {_viewability = value; This.OnPropertyChanged ("ChangeControlVisibility"); }} Private Event PropertyChangedEventHandler PropertyChanged; Public Zero OnPropertyChanged (string propertyName) {If (PropertyChanged! = Null) {PropertyChanged (New, PropertyChangedEventArgs (propertyName)); }}}  

command:

  class MyCommand: ICommand {public delegate void ICommandOnExecute (object parameter); Public representative Bull ICommandOnCanExecute (object parameter); Private ICommandOneExecute _execute; Private ICommandOnCanExecute _canExecute; Public Bull CanExecute (object parameter) {return _canExecute.Invoke (parameter); } Public MyCommand (ICemandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod) {_execute = onExecuteMethod; _canExecute = onCanExecuteMethod; } Public Event EventHandler CanExecuteChanged {addManager.RequerySuggested + = value; } Remove {CommandManager.RequerySuggested - = Value; }} Execute Public Zero (object parameter) {_execute.Invoke (parameter); }}  

Comments