1Mayur
1Mayur

Reputation: 3485

Rendering User Control in Windws Phone 7

I'm new to windows phone 7

I want to ask for your help in some code for:

  1. Im developing and app which has multiple pages. Now I want to put it into a single page using some user control. For example my main page will have a company logo in a grid and a second grid which is empty. The second grid should display the UserControl in which i'll ask user to log in, after log in I want to display another UserControl for some list box and all. Trouble is I'm not getting how can I display and change user control in Page? Just like partial page in asp.net MVC.

  2. Is there any function which gets executed every time user control is changed, like asp.net MVC has "OnActionExecuting". Can we create a UserControl as BaseUserControl and inherit each UserControl from it...is it possible????

Sorry I'm very new to this windows phone.

Upvotes: 0

Views: 722

Answers (2)

Jan K.
Jan K.

Reputation: 2582

I'll try to give you some hints and code to manage your problems. First. It is possible to do all what you want ;). If you want to use only one page (possible not the best practice) you can change your Ui from code. If you have your Page, with MainGrid and two Grids, inside MainGrid. You can access each grid with the x:name property, you had set in xaml. Example:

<Grid x:name="MainGrid">
  <Grid x:Name="LogoGrid"/>
  <Grid x:Name="ContentGrid"/>
</Grid>

Here you can add your userControl as following:

var control = new CustomUserControl();
ContentGrid.Children.Clear(); //maybe delete old Children
ContentGrid.Children.Add(control);

Handling the Events is also easy. Just build it into your UserControl, like a LoginButton and replace the old UserControl with the new one after ButtonClick.

Upvotes: 1

abhinav
abhinav

Reputation: 3217

You can simply change the user control by adding the user control as a child to the container Grid.

MyUserControl myusercontrol = new MyUserControl();
mygrid.Children.Add(myusercontrol);

or to remove,

mygrid.Children.RemoveAt(0); //if you have just one child control.

Upvotes: 1

Related Questions