Autor Beitrag
Christoph1972
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Mi 04.08.10 19:29 
Hallo Leute,

ich möchte z.B. eine TextBox an ein Property binden. Könnte mir jemand einen Hinweis geben, wir ich die Daten in der TextBox aktualisiere?

Bisher sieht das so aus:

Hier ist die Klasse, an dessen Property ich binden möchte.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
public class BusinessObject :INotifyPropertyChanged 
{      
    public event PropertyChangedEventHandler PropertyChanged;           

    private string _Name = "";
    public string Name
    {
        get { return _Name; }
        set { _Name = value;
        if (PropertyChanged != null)
            PropertyChanged(thisnew PropertyChangedEventArgs("Name"));
        }
    }       
}


So binde ich.
ausblenden C#-Quelltext
1:
textBox1.DataBindings.Add(new Binding("Text", business, "Name"));//business=Instanz von BusinessObject					


Mich würde nun interessieren, wie ich eine Aktualisierung mittels PropertyChangedEvent erzwingen kann.

Für Hinweise & Tipps bin ich wie immer Dankbar.

_________________
Gruß
Christoph
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 04.08.10 19:36 
Die Aktualisierung müsste doch automatisch erfolgen, wenn die Eigenschaft geändert wird? :gruebel:

Ansonsten: Da Du Events nicht "von außen" feuern kannst, müsstest Du ansonsten eine Methode schreiben:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
public void RaisePropertyChanged(string PropertyName) {
  var tmp = PropertyChanged;
  if (tmp != null)
    tmp(this,  new PropertyChangedEventArgs(PropertyName));
}

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Mi 04.08.10 21:57 
Hi,

das Event ist kein Problem, wenn ich das Property ändere wird es ausgelöst, die TextBox wird jedoch nicht aktualisiert. Auch in die andere Richtung funktioniert es nicht, also wenn der Text in der TB geändert wird, wird der Set Block nicht angesprochen.

Habe ich den hier alles korrekt??
ausblenden C#-Quelltext
1:
textBox1.DataBindings.Add(new Binding("Text", business, "Name"))					

_________________
Gruß
Christoph
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Mi 04.08.10 22:05 
Du kannst mal einen der überladenen Konstruktoren der Binding-Klasse verwenden, in welchem man explizit den DataSourceUpdateMode setzen kann. Mit dem kannst Du regeln, wann Änderungen im Control in die Property "geschoben" werden.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Mi 04.08.10 22:48 
Ok, danke für heute! Ich werde es mir morgen mal weiter anschauen.

_________________
Gruß
Christoph
Christoph1972 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 690
Erhaltene Danke: 16


VS2015 Pro / C# & VB.Net
BeitragVerfasst: Do 05.08.10 06:53 
Vielen Dank für deine Hilfe!

ausblenden C#-Quelltext
1:
textBox1.DataBindings.Add(new Binding("Text", business, "Name",false ,DataSourceUpdateMode.OnPropertyChanged ))					

_________________
Gruß
Christoph