Question

In a MVVM LOB application where data is persisted via EF service layer I have the following WPF EmployeeView which displays Employee Data via binding to Employee Model object contained in the EmployeeViewModel (Employee object is populated in controller class and persisted through the same class) :

enter image description here

here is xaml:

{
<TextBlock Grid.Column="0" Grid.Row="0" Text="ID:" />
        <TextBox Grid.Column="1" Grid.Row="0" 
                 Text="{Binding Employee.ID,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="50" />

        <TextBlock Grid.Column="0" Grid.Row="1" Text="First Name:" />
        <TextBox Grid.Column="1" Grid.Row="1" 
                 Text="{Binding Employee.FirstName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" />

        <TextBlock Grid.Column="0" Grid.Row="2" Text="Last Name:" />
        <TextBox Grid.Column="1" Grid.Row="2" 
                 Text="{Binding Employee.LastName,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="150" />


        <TextBlock Grid.Column="0" Grid.Row="4" Text="Department:" />
        <ComboBox Grid.Column="1" Grid.Row="4" Width="150" SelectedValuePath="." 
                  SelectedItem="{Binding Employee.Department,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                  ItemsSource="{Binding Employee.DepartmentLookup}" DisplayMemberPath="DepartmentName" />

        <TextBlock Grid.Column="0" Grid.Row="6" Text="Birth Date:" />
        <DatePicker Grid.Column="1" Grid.Row="6" 
                    SelectedDate="{Binding Employee.BirthDate,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <TextBlock Grid.Column="0" Grid.Row="7" Text="Hire Date:" />
        <DatePicker Grid.Column="1" Grid.Row="7" 
                    SelectedDate="{Binding Employee.HireDate,Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

        <Button  Height="25" Width="50" Grid.Column="1" Grid.Row="8" Command="{Binding Path=NewEmpCommand}">
                <TextBlock Margin="1">New</TextBlock>
        </Button>

        <Button Margin="150,5,5,5" Height="25" Width="50" Grid.Column="1" Grid.Row="8" 
                Command="{Binding Path=AddEmpCommand}">
                <TextBlock Margin="1">Add</TextBlock>
        </Button>
}

and the ViewModel

    public class EmployeeViewModel : INotifyPropertyChanged
{


    //NewEmpCommand
    private DelegateCommand _newEmpCommand;
    public DelegateCommand NewEmpCommand
    {
        get
        {
            return _newEmpCommand??
                   (_newEmpCommand= new DelegateCommand(NewEmployee));
        }
    }

    //AddEmpCommand
    private DelegateCommand _addEmpCommand;
    public DelegateCommand AddEmpCommand
    {
        get
        {
            return _addEmpCommand??
                   (_addEmpCommand= new DelegateCommand(AddEmployee));
        }
    }


    //ctor-------------------------------------------------------------------------

    public EmployeeViewModel ()
    {
        Initialize();
    }

    private void Initialize()
    {
     //
    }

    private void NewEmployee()
    {
        this.Employee = null;
    }


    //-------------------------------------------------------------------------
    void AddEmployee()
    {
      //here I send the Employee Model object to the service layer to be persisted 
    }


    //Binding----------------------------------------------------------------------

    private Employee _employee;
    public Employee Employee        
    {
        get { return _employee; }
        set
        {
            if (_employee!= value)
            {
                _employee= value;
                NotifyPropertyChanged("Employee");
            }

        }
    }



   //---------------------------------------------------------
    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

I omitted the unnecessary details. Now I want to click New button to clear all the controls so I use the statement "this.Employee = null;" (correct me if it is not the right way) Then I want to enter the new value into the controls where (to my understanding) it should be assigned to the Employee model object.

to the questions:

1.is this the right way to clear the controls preparing for entering new data

2.When I enter the new data and try to save it (via sending the Employee Object to an EF Service layer to save changes the Employee object is null and the process fail. How to fix this problem?

Was it helpful?

Solution

Converting my comment to answer as requested :

Simply create a brand new Employee object upon button "New" clicked. That will answer both questions:

  1. It is a better way (if I can't claim it as right way) to clear controls preparing for entering new data
  2. That can avoid Employee object is null problem when trying to save

And it is logically correct, click "New" button -> create "New" Employee.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top