Click here to Skip to main content
15,077,641 members
Please Sign up or sign in to vote.
4.00/5 (3 votes)
See more:
I have a same problem: I populate the frames source from the custom class called MasterPageBase.
First time when the main window is loaded, Its defualt constructor calls the MasterPageBase Constructor and the MasterPage Constructor calls the method that populates four(frmHeader,frmFooter,frmSideNavigation and frmMainContent) frames with pages respectively.
Second time when the MasterPageBase custom control is called, is when the use clicks a button from the sideNavigation Frame(which is already been hosted by the MasterPage-mainWindow),the button is suppose to change the content of MainContent frame dynamically according to user's choice,but the mainContent frame stays the same, it doesn't change, still displays the default(old content page) mainContentPage..How do I solve this problem?


//First time when it loads defaults pages:
private void PopulateMasterPageSectionContent()
 {
    masterPage.frmHeader.Navigate(new Uri("Pages/HeaderPage.xaml", UriKind.Relative));
    masterPage.frmSideNavigation.Navigate(new Uri("Pages/SideNavigationPage.xaml", UriKind.Relative));
    masterPage.frmFooter.Navigate(new Uri("Pages/FooterPage.xaml", UriKind.Relative));
    masterPage.frmMainContent.NavigationService.Navigate(new Uri("Pages/ContentPage.xaml", UriKind.Relative));

           }</pre>


Second Time when the use clickes a button from a side navigation frame:
masterPage.frmMainContent = new Frame();
masterPage.frmMainContent.NavigationService.Refresh();

masterPage.frmMainContent.Source = new Uri("Pages/ContentPage.xaml,UriKind.Relative");



My events are properly being wired!
Posted
Updated 11-Mar-11 2:02am
v2
Comments
fjdiewornncalwe 11-Mar-11 18:42pm
   
How about showing your button click handler code. That is likely where your issue lies.
MarvGeek 15-Mar-11 2:47am
   
//Code behind on SideNavigation
private void MemberInfo_Click(object sender, RoutedEventArgs e)
{
OnMembershipButtonClicked(EventArgs.Empty);
}


//Event and Private method on code behind
public event EventHandler MembershipButtonClicked;
private void OnMembershipButtonClicked(EventArgs e)
{
if (MembershipButtonClicked != null)
{
MembershipButtonClicked(this, e);
}
}


//Logic class wof SideNavigation Page
sideNavigationPage.MembershipButtonClicked += new EventHandler(sideNavigationPage_MembershipButtonClicked);



//Event Handler in the logic class
private void sideNavigationPage_MembershipButtonClicked(object sender, EventArgs e)
{
LoadContent(MasterPageBase.ModuleContent.Membership);
}

I think that's all u need..
MarvGeek 15-Mar-11 2:48am
   
private void MemberInfo_Click(object sender, RoutedEventArgs e)
{
OnMembershipButtonClicked(EventArgs.Empty);
}

There is also other method in C# in which User inputs remains saved when u close the page and next time u open it they are updated here is code project article that will help u
User Settings Applied[^]
This has helped me a lot
   
v2
I shall rewrite my problem again..

1.I have Four pages in my wpf application, and a default window page as my mainWindow.
- HeaderPage.xaml (holds and image)
- FooterPage.xaml (holds an image)
- SideNavigationPage.xaml (consits of expander control with buttons as menu buttons)
- ContentPage.xaml (default home page)

2.Each page has its own logic custom class
- HeaderPageLogic.cs
- FooterPageLogic.cs
- *SideNavigationPageLogic.cs*
- ContentPageLogic

NB: The purpose of these logic classes is to define the logic that happens on each page, for instance to wire up the events on buttons

I shall put more emphises on SideNavigationPage and Its Logic class


3.MainWindow's name is called MasterPage
3.1 It consists of of four frames.
- Header frame that will hold header page. (frmHeader)
- footer frame that will hold the footer page. (frmFooter)
- SideNavigation frame that will hold sideNavigation page. (frmSideNavigation)
- Main content frame that will hold a dynamic content pages. (frmMainContent)


4.At first when the Mainwindow runs
VB
MasterPage.xaml.cs

private MasterPageBase masterPageBase;

//Default Construstor
C#
public MainWindow()
       {
           InitializeComponent();
           Initialize(); // calls the local method

       }


//User defined Constructor
C#
public MainWindow(Page subPage,MasterPagePractice.MasterPageBase.PageSections pageSection)
       {
           InitalizeSubPages(subPage, pageSection);
       }




C#
private void Initialize()
       {
           masterPageBase = new MasterPageBase(this);//Instiates an object of MasterPageBase

       }


XML
<pre lang="cs">private void InitalizeSubPages(Page subPage, MasterPagePractice.MasterPageBase.PageSections pageSection)
      {
          masterPageBase = new MasterPageBase(this, subPage, pageSection);

      }</pre>



MasterPageBase.cs
private MainWindow masterPage;

C#
public MasterPageBase(MainWindow masterPage)
           {
               this.masterPage = masterPage;
               PopulateMasterPageSectionContent();
           }


C#
public MasterPageBase(MainWindow masterPage, Page subPage, PageSections pageSection)
          {
              this.masterPage = masterPage;
              Initialize(subPage, pageSection);
          }



//Enum
C#
public enum PageSections : int
           {
               Header = 0,
               SideNavigation = 1,
               Content = 2,
               Footer = 3
           }




C#
private void PopulateMasterPageSectionContent()
          {
              masterPage.frmHeader.Navigate(new Uri("Pages/HeaderPage.xaml", UriKind.Relative));
              masterPage.frmSideNavigation.Navigate(new Uri("Pages/SideNavigationPage.xaml", UriKind.Relative));
              masterPage.frmFooter.Navigate(new Uri("Pages/FooterPage.xaml", UriKind.Relative));
              masterPage.frmMainContent.Navigate(new Page());

          }



Everything loads fine at first.Now since the compiler will call the constructor of each page navigated by the frames respectively, I have decided to have a second constructor in MainWindow (MasterPage.xaml) that accept the sub page and page section enum as parameters.The reason for doing this is to make the sub pages to have an access to MasterPageBase class since is the controller of everthing, even the logic classes of the subpages are being controlled by MasterPageBase class.

As I have mentioned above that when frames navigate to a certain page per Uri, after everything has been initialized and set according to the logic of the application, the compiler runs the default constructors of each page that is being navigated as per Uri by frames.So my point is that, it is useless during your programme execution to wire up the events on your pages using referrences, where else at the end the compiler will run their constructors which will unwire those events and start everything on those pages at null.(I wonder what is the reason for that???)

I have mentioned that the main focus will be on SideNavigation page and It's logic class.so below is a code that calls back the MasterPage constructor from SideNavigation page(note that I'm forced to this since the compiler calls each page after everything).


PROBLEM!!! When the button is clicked it suppose to change frame main content, which it doesn't, so I have put everything below, so that you can follow my logic.


SideNavigationPage.xaml
XML
<Page x:Class="MasterPagePractice.Pages.SideNavigationPage"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d"
      d:DesignHeight="300" d:DesignWidth="300"
    Title="SideNavigationPage">

    <StackPanel>
        <Expander Name="MembershipInfo" Header="Membership">
            <Button Name="MemberInfo" Height="40" Click="MemberInfo_Click">Members</Button>
        </Expander>
        <Expander Name="BaptismalInfo" Header="Baptismal">
            <Button Name="BaptismInfo" Height="40" Content="Baptismal"></Button>
        </Expander>
    </StackPanel>
</Page>



SideNavigationPage.xaml.cs
C#
public SideNavigationPage()
      {
          InitializeComponent();
          Initialize();
      }



C#
public event EventHandler MembershipButtonClicked;
      private void OnMembershipButtonClicked(EventArgs e)
      {
          if (MembershipButtonClicked != null)
          {
              MembershipButtonClicked(this, e);
          }
      }



C#
private void MemberInfo_Click(object sender, RoutedEventArgs e)
     {
         OnMembershipButtonClicked(EventArgs.Empty);
     }





C#
private void Initialize()
      {
          MainWindow mainWindow = new MainWindow(this, MasterPageBase.PageSections.SideNavigation);
      }



MasterPage.xaml.cs
C#
public MainWindow(Page subPage,MasterPagePractice.MasterPageBase.PageSections pageSection)
      {
          InitalizeSubPages(subPage, pageSection);
      }


C#
private void InitalizeSubPages(Page subPage, MasterPagePractice.MasterPageBase.PageSections pageSection)
      {
          masterPageBase = new MasterPageBase(this, subPage, pageSection);

      }




MasterPageBase.cs
C#
public MasterPageBase(MainWindow masterPage, Page subPage, PageSections pageSection)
          {
              this.masterPage = masterPage;
              Initialize(subPage, pageSection);
          }



C#
private void Initialize(Page initilizePage, PageSections pageSection)
         {
             switch (pageSection)
             {
                 case PageSections.SideNavigation:
                     SideNavigationPageLogic sideNavigationLogic = new SideNavigationPageLogic(this, (SideNavigationPage)initilizePage);
                     break;
             }

         }


C#
public void DisplayContent(object uriObject)
         {

             masterPage.frmMainContent = new Frame();
             masterPage.frmMainContent.Navigate(new Uri("Pages/ContentPage.xaml", UriKind.Relative));

         }




SideNavigationLogic.cs
private MasterPageBase masterPageBase;
private SideNavigationPage sideNavigationPage;

C#
public SideNavigationPageLogic(MasterPageBase masterPageBase, SideNavigationPage sideNavigationPage)
           {
               this.masterPageBase = masterPageBase;
               this.sideNavigationPage = sideNavigationPage;
               Initialize();
           }


C#
private void Initialize()
           {
               SubscribeToEvents();
           }


C#
private void SubscribeToEvents()
            {
                sideNavigationPage.MembershipButtonClicked += new EventHandler(sideNavigationPage_MembershipButtonClicked);
            }


C#
private void sideNavigationPage_MembershipButtonClicked(object sender, EventArgs e)
           {
               LoadContent(MasterPageBase.ModuleContent.Membership);
           }



C#
private void LoadContent(MasterPageBase.ModuleContent moduleContent)
         {
             switch (moduleContent)
             {
                 case MasterPagePractice.MasterPageBase.ModuleContent.Membership:
                     MainWindow.ModuleContent = MasterPagePractice.MasterPageBase.ModuleContent.Membership;
                     memberControl = new MembershipContainerControl();
                     break;
             }

             masterPageBase.DisplayContent(memberControl);
         }




My question is why the main content frame remains the same, and doesn't want to change its content.I have been searching for an answer for weeks now..please help me.Thank you
   
Comments
amitkarnik2211 16-Mar-11 7:06am
   
Kindly put this entire thing in Question or else post the entire question again to get the quick response and the answer that I posted earlier was due to my misunderstanding sorry about taht

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900