1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace praktika6
- {
- public partial class Form2 : Form
- {
- public Form2()
- {
- InitializeComponent();
- Person person = new Person { Name = "Tom", Age = 18 };
- Label label1 = new Label();
- label1.Location = new Point(12, 10);
- label1.AutoSize = true;
- Controls.Add(label1);
- Label label2 = new Label();
- label2.Location = new Point(12, 40);
- label2.AutoSize = true;
- Controls.Add(label2);
- Button button = new Button();
- button.Location = new Point(12, 80);
- button.Text = "Change";
- button.AutoSize = true;
- Controls.Add(button);
- // изменяем свойство Name
- button.Click += (o, e) => person.Name = "admin";
- label1.DataBindings.Add(new Binding("Text", person, "Name", false, DataSourceUpdateMode.OnPropertyChanged));
- label2.DataBindings.Add(new Binding("Text", person, "Age", false, DataSourceUpdateMode.OnPropertyChanged));
- }
- }
- public class Person : INotifyPropertyChanged
- {
- string name = "";
- int age;
- public event PropertyChangedEventHandler? PropertyChanged;
- public string Name
- {
- get => name;
- set
- {
- if (name != value)
- {
- name = value;
- OnPropertyChanged();
- }
- }
- }
- public int Age
- {
- get => age;
- set
- {
- if (age != value)
- {
- age = value;
- OnPropertyChanged();
- }
- }
- }
- public void OnPropertyChanged([CallerMemberName] string prop = "")
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
- }
- }
- }
|