DataBinding issue
-
Hello people, I have the following problem. I have a class with a property like in the below example:
class myClass:INotifyPropertyChanged
{
private string theProp;public string TheProp
{
get{ return theProp; }
set
{
if( string.IsNullOrEmpty(value) )
theProp = "Another Value";
else
theProp = value;
NotifyPropertyChanged("TheProp");
}
}
/// And a lot of other stuff
}So in case an empty or null is set I set the internal value of the property to a default string. The object is doing this just fine. But the problem I have is with a textBox that is bound to this property. When I delete the text in the textbox, the textbox does not represent the different value. Probably I'm doing something wrong with the binding, it seems like a common way to set a different value, so I suppose there must be a way to make this work. thanks for any help you might provide. Davy
-
Hello people, I have the following problem. I have a class with a property like in the below example:
class myClass:INotifyPropertyChanged
{
private string theProp;public string TheProp
{
get{ return theProp; }
set
{
if( string.IsNullOrEmpty(value) )
theProp = "Another Value";
else
theProp = value;
NotifyPropertyChanged("TheProp");
}
}
/// And a lot of other stuff
}So in case an empty or null is set I set the internal value of the property to a default string. The object is doing this just fine. But the problem I have is with a textBox that is bound to this property. When I delete the text in the textbox, the textbox does not represent the different value. Probably I'm doing something wrong with the binding, it seems like a common way to set a different value, so I suppose there must be a way to make this work. thanks for any help you might provide. Davy
public partial class Form1 : Form
{
myClass o = new myClass();public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { this.textBox1.DataBindings.Add("Text", o, "TheProp", false, DataSourceUpdateMode.OnPropertyChanged, string.Empty); } } class myClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string theProp; public string TheProp { get { return theProp; } set { if (string.IsNullOrEmpty(value)) theProp = "Another Value"; else theProp = value; NotifyPropertyChanged("TheProp"); } } private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }
-
public partial class Form1 : Form
{
myClass o = new myClass();public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { this.textBox1.DataBindings.Add("Text", o, "TheProp", false, DataSourceUpdateMode.OnPropertyChanged, string.Empty); } } class myClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private string theProp; public string TheProp { get { return theProp; } set { if (string.IsNullOrEmpty(value)) theProp = "Another Value"; else theProp = value; NotifyPropertyChanged("TheProp"); } } private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }