WPF: How to pass data to PAGE from MainWindow in codebehind?

Viewed 50

Hi members, I have a simple question about how to pass data to Page from MainWindow.

I searched on google/ and here... But I am not found exact answer.

I would like modify, change the Page1( SW_GUI.xaml) elements such as label content, or later change other GUI element content. However I only can reached (myself) this way: firstly make change on label of SW_GUI.xaml and then create instance of this Page(SW_GUI.xaml) and load in a frame of MainWindow content. But if this Page loaded once, I cannot modify /update a label automatically, without loading the SW_GUI Page again into the frame. You can find the very simple and initial code below.

MainWindow.xaml.cs file

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // ACTION one - just try reason



        SW_GUI sW_GUI = new SW_GUI();
        sW_GUI.RESULT_MAKER(true);

        CenterFrame.Content = sW_GUI;

        
        


    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
    
    // ACTION two - just try reason
        SW_GUI sW_GUI = new SW_GUI();

        sW_GUI.RESULT_MAKER(false);
        CenterFrame.Navigate(sW_GUI);

    }
}

-----------------
PAGE1 aka SW_GUI

SW_GUI.xaml.cs file

 public partial class SW_GUI : Page
{
    public SW_GUI()
    {
        InitializeComponent();
    }



    public void RESULT_MAKER ( bool results)
    {
        if (results==true)
        {

            RESULT_BOX.Background = new SolidColorBrush(Color.FromRgb(0, 245, 95));

            RESULT_BOX.Text = "(PASS)";
           


        }
        else
        {

            RESULT_BOX.Background = new SolidColorBrush(Color.FromRgb(245, 53, 0));

            RESULT_BOX.Text = "(FAIL)";
        }

       
    }
}

SHORT logic: Button_Click on mainwin call the sW_GUI.RESULT_MAKER method and make very simple change on a label. Then, I load the sW_GUI instance into the CenterFrame.Content.

QUESTION: Ca you provide a little guide or give a simple example what I have to add, change to achieve that I don't need to load the page into the frame each case when I want to update a label content etc.

Many thanks for it.

1 Answers

Hello I have made a small sample in MainWindow.cs:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private SW_GUI SW_GUIPage = new SW_GUI();  

        private void ActivateSW_GUI(bool SetResult)
        {
            SW_GUI SW = SW_GUIPage;
            SW.RESULT_MAKER(SetResult);                                    
            if (!(CenterFrame.Content is SW_GUI))
            {
                CenterFrame.Content = SW_GUIPage;                         
            }           

        }

        int ActionSetter = 0;

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (ActionSetter == 0)
            {
                ActivateSW_GUI(true);
                ActionSetter = 1;
            }
            else if (ActionSetter == 1)
            {
                ActivateSW_GUI(false);
                ActionSetter = 0;
            }
        }

    }
Related