Fabio Martins
Fabio Martins

Reputation: 55

Check if textbox is empty in a WPF form from another class C#

I have an WPF form with 3 textbox named FirstName LastName and GPA, and i want to check their content before inserting into the database. But i am trying to do this in another class. How can i make to access the textbox control from my class? the WPF window is named AddStudent and the class i want to make the check is named Consiste. I made Consiste static so i can use it on my AddStudent without creating an instance.

Upvotes: 0

Views: 2962

Answers (3)

Sean U
Sean U

Reputation: 6850

If you're following the Model-View-ViewModel design pattern, which is recommended since that's the one WPF was designed for, then the general approach is like this:

First, create a ViewModel class to be the actual store of data. (The ViewModel should probably implement INotifyPropertyChanged, but I'll skip it here for the sake of brevity.)

public class MyFormViewModel
{
    public string FirstName {get; set;}
    public string LastName {get; set;}
    public float GPA {get; set}
}

This class's properties are public, so they're visible from everywhere.

Then, create a form and use bindings to connect its fields to your properties:

<Window x:Class="MyNamespace.MyForm">
    <StackPanel>
        <TextBox Text={Binding FirstName}/>
        <TextBox Text={Binding LastName}/>
        <TextBox Text={Binding Gpa}/>
    </StackPanel>
</Window>

And finally, when you instantiate the form create a new ViewModel to use as the form's DataContext:

var myFormContext = new MyFormViewModel();
var dialog = new MyForm { DataContext = myFormContext };

The reason for going through this extra effort instead of just coding against the UI directly is that it helps keep your code more organized. On down the road it's easier to reuse the same ViewModel classes with multiple views, or to rearrange your user interface without creating such a cascade of changes going through all your code. So for the long run following the MVVM pattern is a very good habit to get into right away.

As a concrete example of something that's easier if you structure your code this way, there's already a data validation mechanism in WPF that's built to work with MVVM code: the IDataErrorInfo interface. You can implement it in your ViewModel without too much trouble, and then add some markup to your bindings that tell them to expect validation, and you can be done there - though if you want to customize how the user is warned about errors, controls have an ErrorTemplate that you can style to suit.

Upvotes: 0

Chris
Chris

Reputation: 4671

In general it's not considered a good idea to be passing your UI elements into your backend code (even helpers within your UI project).

Presumably the AddStudent form is calling a method on the Consiste class. Rather than having the Consiste class reach back into the form to get at the textboxes, your method call should be passing the values to the Consiste class.

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

In you Util class, you must just have method with input parameter

public static bool Validate(string firstName, string lastName,string gPA)
{
   if(firstName.Length < 1)  
   .....

   return true;
}

And call this static class Util

Util.Validate(FirstName.Content, LastName.Content, GPA.Content);

Upvotes: 1

Related Questions