locked
WPF Calling same method name across different page controls RRS feed

  • Question

  • Hi there. In my WPF project, I have a window that contains a page control. the window allows for the page control to load a series of pages. Each page allows the user to edit data. When the user enters data into a page and then navigates away from that page (using controls on the main window) I want the data to be saved to the database before navigation occurs. Its important that the save happens automatically. We don't want the user to click a save button on every page of the application.

    For example, lets consider three parts of the UI: Mainwindow, Page1 and Page2

    Assume I create methods for each page, each called Savedata() for page1 and for page2. The user enters data on page 1, then clicks on a button on the main form to navigate to page 2. For this event on the main window, Normally, I would envision something like:

    Dim CurrentPage as page = MyFrame.Content  ' can I even do this?

    Currentpage.SaveData()

    myframe.content = new uri("Page2")....

    the problem is Currentpage could contain a page1 or page2 object, each with their own SaveData() method. How can I correctly call the savedata method from the current page object? Thank you!

    Monday, January 25, 2016 5:12 PM

Answers

  • You should define an interface with the SaveData method, implement that in all your pages.

    Call it IPage.

    You can then cast the content of your framework as an IPage and call it's SaveData method without knowing which page it is.

        interface IPage
        {
            void SaveData();
        }

    and

        public partial class Page1 : Page, IPage
        {
            public Page1()
            {
                InitializeComponent();
            }
            public void SaveData()
            {
                // save your data
            }
        }

    You would want some method to do your navigation that calls savedata before you navigate each time rather than a straight click handler.

    But in principle:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                IPage page = TheFrame.Content as IPage;
                page.SaveData();
            }


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, January 25, 2016 5:36 PM

All replies

  • You should define an interface with the SaveData method, implement that in all your pages.

    Call it IPage.

    You can then cast the content of your framework as an IPage and call it's SaveData method without knowing which page it is.

        interface IPage
        {
            void SaveData();
        }

    and

        public partial class Page1 : Page, IPage
        {
            public Page1()
            {
                InitializeComponent();
            }
            public void SaveData()
            {
                // save your data
            }
        }

    You would want some method to do your navigation that calls savedata before you navigate each time rather than a straight click handler.

    But in principle:

            private void Button_Click(object sender, RoutedEventArgs e)
            {
                IPage page = TheFrame.Content as IPage;
                page.SaveData();
            }


    Hope that helps.

    Technet articles: WPF: Layout Lab; All my Technet Articles

    Monday, January 25, 2016 5:36 PM
  • Simply Awesome! Thanks Andy!
    Tuesday, January 26, 2016 10:56 PM