Friday 15 February 2013

Modal Window Use In LightSwitch

Use Modal Window to add and edit records in the lightswitch

1 In your screen Add new group, it will Add Rows layout change it to be modal window "this window will hold your screen for add/edit" Rename this modal window to be AddEditCustomer
2 Change its Is Visible property to false
3 In its command bar Add new button twice one will be btnOk, the other will be btnCancel, then Below it drag the Customers query
4 Add property of type Customer to the designer call it NewCustomer
5 Point to your Customers list or grid command bar right click on Add button and select Edit Execute Code, do the same for Edit Button
the 2 functions code should be like this :
 
partial void CustomerListAddAndEditNew_Execute()
        {
            // Write your code here.
            NewCustomer = new Customer();
            this.OpenModalWindow("AddNewCustomer");
            this.FindControl("AddNewCustomer").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(NewCustomer_WindowAvailable);
        }
partial void CutomerListEditSelected_Execute()
        {
            // Write your code here.
            NewCustomer = this.Customers.SelectedItem;
            this.OpenModalWindow("AddNewCutomer"); // modal window name
            this.FindControl("AddNewCutomer").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(NewCusomr_WindowAvailable);
        }

void NewCustomer_WindowAvailable(object sender, ControlAvailableEventArgs e)

{



this.FindControl("AddNewCustomer").ControlAvailable -= new EventHandler<ControlAvailableEventArgs>(NewCustomer_WindowAvailable);



((ChildWindow)e.Control).Closed += new EventHandler(NewCustomer_Closed);



}

void NewCustomer_Closed(object sender, EventArgs e)

        {



            ((ChildWindow)sender).Closed -= new EventHandler(NewCustomer_Closed);



            if (!((ChildWindow)sender).DialogResult.HasValue)

                 CancelAddCustomerChanges();

               CloseAddCustomerWindow();



        }

private void CloseAddCustomerWindow()

        {

             this.CloseModalWindow("AddNewCustomer");

           

        }

        private void CancelAddCustomerChanges()

        {

            NewCustomer.Details.DiscardChanges();

        }

partial void btnOk_Execute()

        {

            // Write your code here.

            this.Customers.SelectedItem = this.NewCustomer;

            CloseAddCustomerWindow();

        }



        partial void btnCancel_Execute()

        {

            // Write your code here.

            CancelAddCustomerChanges();

            CloseAddCustomerWindow();

        }

partial void btnSubmit_CanExecute(ref bool result)

        {

            // Write your code here.

            result =  NewCustomer!= null && NewCustomer.CustomerName!=null && Any Mandatory Field in Customer entity!=null;

        }

1 comment:

jamsolo said...

Do not forget to put the reference in:

using using System.Windows.Controls;