wszystkow kontekscie Windows.Forms
mam kontrolke AddressControl, na niej m.in. text box na nazwe ulicy tbStreet
mam tez wlasciwosc Address - i pole protected Address _address - ktore zwaraca/ustawia properta Address
chce uzywajac mechanizmow Binding "polaczyc" textbox z polem z Address
klasa Address implementuje INotifyPropertyChanged
wiec skoro ControlUpdateMode i DataSourceUpdateMode sa na OnPropertyChanged, to spodziewalbym sie ze kiedy w textbox tbStreet ktos cos wpisze to w _address.Street zostanie to zaktualizowane oraz ze kiedy ktos (z kodu) zmodyfikuje addressCtrl1.Address.Street = "bla"; to wartosc w tbStreet sie zaktualizuje
czy zle mysle, czy tylko cos zle zrobilem? pewnie gdzies jakas glupote zrobilem, ale juz nie widze gdzie
public partial class AddressControl : UserControl
{
public AddressControl()
{
InitializeComponent();
_address = new Address();
Binding b = new Binding("Text", _address, "Street", true, DataSourceUpdateMode.OnPropertyChanged);
b.ControlUpdateMode = ControlUpdateMode.OnPropertyChanged;
tbStreet.DataBindings.Add(b);
}
protected Address _address;
public Address Address
{
get
{
if (_address == null) _address = new Address();
return _address;
}
set
{
_address = value??new Address();
}
}
}
public class Address : INotifyPropertyChanged
{
protected string _street;
public string Street
{
get { return _street; }
set
{
if (_street != value)
{
_street = value;
NotifyPropertyChanged("Street");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}