Bindowanie properties

0

Dzien dobry,

Mam problem z bindowaniem.
Ponizesze bindowanie robione jest w Form_Show
lblServerName - to label.

Binding BindlblServerName = new Binding("Text", OPCInterface, "ServerName");
BindlblServerName.Format += (sender1, e1) => e1.Value = "OPC DA: " + e1.Value;
 lblServerName.DataBindings.Add(BindlblServerName);

OPCInterface to obiekt mojej wlasnej klasy OPCServerInterface:

public class OPCServerInterface {
     public delegate void dLogMsg(string aProcess, bool aExtDiag, string aLine);
     public dLogMsg LogMsg;
     public bool IsConnected { get { return _OpcSrv.isConnectedDA; } }
     public string ServerName { get { return _OpcSrv.ServerName; } }
    ....
}


W momencie bindowanie OPCInterface._OpcSrv == null
wiec lblServerName.Text == "OPC DA: "
Potem jedank nazwa serwera sie zmienia, a lblServerName.Text juz nie ;(

Nawet jak dodalem dodatkowa zmienna, zeby properities ServerName nie wskazywalo na null podczas bindowania to I tak nie dziala

0

Dzien dobry raz jeszcze,

Sprawa mniej wiecej rozwiazana. Trzeba zaimplementowac INotifyPropertyChanged Interface, bo samo niestety automatycznie sie nie dzieje.

public class OPCServerInterface: INotifyPropertyChanged
{
     ....
     public event PropertyChangedEventHandler PropertyChanged;
     public string ServerName {  get { return _OpcSrv.ServerName; }}
    
     private void NotifyPropertyChanged([CallerMemberName]String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
     ....
}

I wywolac wszedzie gdzie zmienie sie server:

NotifyPropertyChanged("ServerName");

Mialem nadzieje na wiekszy automatyzm ;)

0

Na GitHubie są różne biblioteki pozwalające na automatyczne implementowanie INotifyPropertyChanged :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1