当前位置:网站首页>WPF command directive and inotifypropertychanged

WPF command directive and inotifypropertychanged

2022-06-23 06:59:00 Big fight

MainViewMode.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WPFTest1
{
    // INotifyPropertyChanged  Give Way Name and Title After the change , The front desk can display changes 
    public class MainViewMode : INotifyPropertyChanged
    {
        public MainViewMode() 
        {
            //  Constructor initializes values that change dynamically 
            Name = "Hello";
            Title = " title ";
            //  Use 
            ShowCommand = new MyCommand(show);
        }

        //  After definition, the background can Binding
        public MyCommand ShowCommand { get; set; }

        //  attribute Name
        private string name;
        public string Name 
        { get { return name; }
          set { name = value;
                //  After the property changes, the foreground display changes synchronously 
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
            } 
            
        }

        //  attribute Title
        private string title;
        public string Title
        {
            get { return title; }
            set
            {
                title = value;
                //  After the property changes, the foreground display changes synchronously 
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Title"));
            }

        }

        //  Make a worthwhile change 
        public void show() 
        {
            Name = " Click the button ";
            Title = new Random().Next(1, 10).ToString();
            MessageBox.Show(Name);
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}

MainWindow.xaml

    <Grid>
        <StackPanel>
            <TextBox Text="{Binding Name}"/>
            <TextBox Text="{Binding Title}"/>
            <Button Content="Show" Command="{Binding ShowCommand}"/>
        </StackPanel>
        
    </Grid>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFTest1
{
    /// <summary>
    /// MainWindow.xaml  Interaction logic of 
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //  Define context data , send MainViewMode Become its data source 
            this.DataContext = new MainViewMode();
        }

    }
}

MyCommand.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace WPFTest1
{
    //  Customize a Command, Must inherit ICommand
    public class MyCommand : ICommand

    {
        //  A commission 
        Action executeAction;
        // MainViewMode.cs in  MainViewMode Delegate passed in by constructor   from  MyCommand  Construct a method to receive 
        public MyCommand(Action action) 
        {
            //  Receiving entrustment 
            executeAction = action;
        }

        //  Implementation interface  ICommand  It comes with you 
        public event EventHandler CanExecuteChanged;

        //  Implementation interface  ICommand  It comes with you ,return true; I wrote it myself ,true For executables 
        //  After the program is executed , Click button ,  The value returned by the tool here determines whether it can execute 
        public bool CanExecute(object parameter)
        {
            return true;
        }

        //  After the program is executed ,  If it can be executed, start to execute , Execution is  MyCommand Constructor side   An incoming delegate 
        public void Execute(object parameter)
        {
            //  Perform entrusted :show ( from  MainViewMode.cs Pass in )
            executeAction();
        }
    }
}

Before clicking the button :

 

After clicking the button :Show The text box above changes and a pop-up window appears

 

  Process carding :

1. Create a page , Back end ViewMode Set properties , front end xaml Binding properties

2. Customize MyCommand  Instructions , Bind the button to the instruction , This command is triggered when pressed

3. Press the button , Perform binding Instructions , Instructions Calling method Change the value , With entrust In the form of Pass on , Last perform Pass it on Method , It seems to be executing instructions Command, the truth is that Execution is passed as a delegate to MyCommand Method in

4. After the command is executed , Properties change synchronously , Make the front display change synchronously

原网站

版权声明
本文为[Big fight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230534394767.html