Efitap
October 21st, 2008, 01:06 AM
Project type is Windows forms application.
I have the following issue:
I want to unit test my GUI as far as I can, thus I wrote:
namespace Presentation
{
public class LoginForm : INotifyPropertyChanged
{
private string _password = string.Empty;
// Declares an event to fire, should we want to report that a property has changed
public event PropertyChangedEventHandler PropertyChanged;
// Case sensitive comparison is performed on the password prior to setting the new value
public string Password
{
get { return _password; }
set
{
if( string.Compare( _password, value, false ) != 0 )
{
_password = value;
if( PropertyChanged != null )
{
PropertyChanged( this, new PropertyChangedEventArgs( "Password" ) );
}
}
}
}
public bool OkButtonEnabled
{
get
{
return ! ( string.IsNullOrEmpty( Username ) || string.IsNullOrEmpty( Password ) );
}
}
}
}
(Code chopped for legibility)
Now, I have a corresponding windows form that I want to use this class with, I create the button and text box, and find the databinding controls.
The first time that I reference it, I am asked to create a data binding source, no problem, I navigate through the namespaces and end up with a loginFormBindingSource object.
Then I proceed. In properties for the Password, I bind the text property to the loginFormBindingSource's Password property.
For the button, I go to "advanced" and choose to bind the "Enabled" property to the "OkButtonEnabled" property in the binding source.
Compiling and running is without error, however, the data binding does not work. I must be missing something important, like a constructor, so I tried
to initialize the binding source, but to no avail.
If I remove all GUI bindings and instead opt to bind manually in the form's constructor like this:
public partial class Form1 : Form
{
LoginForm codeBehindLoginForm = new LoginForm( );
public Form1()
{
InitializeComponent();
tbPassword.DataBindings.Add ( "Text", codeBehindLoginForm, "Password" );
btnOk.DataBindings.Add ( "Enabled", codeBehindLoginForm, "OkButtonEnabled" );
}
}
...then everything works as expected.
Can anyone please enlighten me as to how I can get the GUI binding, using properties to work? I am almost sure that the only thing missing is some form of initalization, I'm just not sure where. A search here, and on google returns only examples of binding to data sources.
In advance, thanks for reading.
I have the following issue:
I want to unit test my GUI as far as I can, thus I wrote:
namespace Presentation
{
public class LoginForm : INotifyPropertyChanged
{
private string _password = string.Empty;
// Declares an event to fire, should we want to report that a property has changed
public event PropertyChangedEventHandler PropertyChanged;
// Case sensitive comparison is performed on the password prior to setting the new value
public string Password
{
get { return _password; }
set
{
if( string.Compare( _password, value, false ) != 0 )
{
_password = value;
if( PropertyChanged != null )
{
PropertyChanged( this, new PropertyChangedEventArgs( "Password" ) );
}
}
}
}
public bool OkButtonEnabled
{
get
{
return ! ( string.IsNullOrEmpty( Username ) || string.IsNullOrEmpty( Password ) );
}
}
}
}
(Code chopped for legibility)
Now, I have a corresponding windows form that I want to use this class with, I create the button and text box, and find the databinding controls.
The first time that I reference it, I am asked to create a data binding source, no problem, I navigate through the namespaces and end up with a loginFormBindingSource object.
Then I proceed. In properties for the Password, I bind the text property to the loginFormBindingSource's Password property.
For the button, I go to "advanced" and choose to bind the "Enabled" property to the "OkButtonEnabled" property in the binding source.
Compiling and running is without error, however, the data binding does not work. I must be missing something important, like a constructor, so I tried
to initialize the binding source, but to no avail.
If I remove all GUI bindings and instead opt to bind manually in the form's constructor like this:
public partial class Form1 : Form
{
LoginForm codeBehindLoginForm = new LoginForm( );
public Form1()
{
InitializeComponent();
tbPassword.DataBindings.Add ( "Text", codeBehindLoginForm, "Password" );
btnOk.DataBindings.Add ( "Enabled", codeBehindLoginForm, "OkButtonEnabled" );
}
}
...then everything works as expected.
Can anyone please enlighten me as to how I can get the GUI binding, using properties to work? I am almost sure that the only thing missing is some form of initalization, I'm just not sure where. A search here, and on google returns only examples of binding to data sources.
In advance, thanks for reading.