2014년 3월 21일 금요일

INotifyPropertyChanged Test

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6 {
    public partial class UCtlCompA : UserControl, 
                                     INotifyPropertyChanged {
        public UCtlCompA() {
            InitializeComponent();
            this.textBox2.DataBindings.Add("Text", 
                                           this, 
                                           "SumName", 
                                           false, 
                                           DataSourceUpdateMode.OnPropertyChanged);
        }

        private void txtTitle_TextChanged(object sender, EventArgs e) {
            ProdName = txtTitle.Text;
            SumName = ProdName + " / " + UserName;
        }

        private void textBox1_TextChanged(object sender, 
                                          EventArgs e) {
            UserName = textBox1.Text;
            SumName = ProdName + " / " + UserName;
        }

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") {
            if (PropertyChanged != null) {
                PropertyChanged(this, 
                                new PropertyChangedEventArgs(propertyName));
            }
        }

        public string ProdName {
            get { return this._prodname; }
            set {
                if (value != this._prodname) {
                    this._prodname = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string UserName {
            get { return this._username; }
            set {
                if (value != this._username) {
                    this._username = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public string SumName {
            get { return this._sumname; }
            set {
                if (value != this._sumname) {
                    this._sumname = value;
                    NotifyPropertyChanged();
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private string _prodname = string.Empty;
        private string _username = string.Empty;
        private string _sumname = string.Empty;

    }
}