zoukankan      html  css  js  c++  java
  • WPF委托实现方法回调(刷新父窗体)

    MainWindow.xaml(父窗体)

    <StackPanel>
        <Label Content="{Binding Message}"/>
        <Button Content="Input User Name" Command="{Binding OpenSubWindowCommand}"/>
    </StackPanel>

    MainWindow.xaml.cs

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MainWindowViewModel();
    }

    MainWindowViewModel.cs

    class MainWindowViewModel : INotifyPropertyChanged
    {
        string message;
    
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public string Message
        {
            get => message;
            set
            {
                message = value;
                OnPropertyChanged("Message");
            }
        }
    
        public ICommand OpenSubWindowCommand { get; set; }
    
        public MainWindowViewModel()
        {
            OpenSubWindowCommand = new CommandHandler(OpenSubWindow);
        }
    
        void OpenSubWindow()
        {
            SubWindow subWindow = new SubWindow((name) => { Message = $"hello {name}"; });
            subWindow.Show();
        }
    }

    SubWindow.xaml(子窗体)

    <StackPanel>
        <TextBox Text="{Binding Name}"/>
        <Button Content="Send" Command="{Binding SendCommand}"/>
    </StackPanel>

    SubWindow.xaml.cs

    public SubWindow(Action<string> callback)
    {
        InitializeComponent();
        this.DataContext = new SubWindowViewModel(callback, this);
    }

    SubWindowViewModel.cs

    class SubWindowViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        string name;
        public string Name
        {
            get => name;
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }
    
        Action<string> Callback { get; set; }
        SubWindow Window { get; set; }
    
        public SubWindowViewModel(Action<string> callback, SubWindow window)
        {
            SendCommand = new CommandHandler(Send);
            Callback = callback;
            Window = window;
        }
    
        public ICommand SendCommand { get; set; }
    
        void Send()
        {
            if (Callback != null)
            {
                Callback(name);
                Window.Close();
            }
        }
    }
    把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。
  • 相关阅读:
    车厢重组
    军事机密
    士兵站队
    归并排序
    输油管道
    冒泡排序
    快排
    烦人的幻灯片(确实烦人啊)
    奖金(类拓扑排序)
    能量项链
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/15766668.html
Copyright © 2011-2022 走看看