简体   繁体   English

WPF:如何在代码隐藏中将数据从 MainWindow 传递到 PAGE?

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

Hi members, I have a simple question about how to pass data to Page from MainWindow.大家好,我有一个关于如何从 MainWindow 向 Page 传递数据的简单问题。

I searched on google/ and here... But I am not found exact answer.我在 google/ 和这里搜索...但我没有找到确切的答案。

I would like modify, change the Page1( SW_GUI.xaml) elements such as label content, or later change other GUI element content.我想修改、更改 Page1( SW_GUI.xaml) 元素,例如 label 内容,或者稍后更改其他 GUI 元素内容。 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.但是我只能通过这种方式到达(我自己):首先对 SW_GUI.xaml 的 label 进行更改,然后创建此页面的实例(SW_GUI.xaml)并加载一帧 MainWindow 内容。 But if this Page loaded once, I cannot modify /update a label automatically, without loading the SW_GUI Page again into the frame.但是,如果此页面加载一次,我无法自动修改/更新 label,而无需再次将 SW_GUI 页面加载到框架中。 You can find the very simple and initial code below.您可以在下面找到非常简单的初始代码。

MainWindow.xaml.cs file MainWindow.xaml.cs 文件

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 SW_GUI.xaml.cs 文件

 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. SHORT 逻辑:在 mainwin 上的 Button_Click 调用 sW_GUI.RESULT_MAKER 方法并对 label 进行非常简单的更改。 Then, I load the sW_GUI instance into the CenterFrame.Content.然后,我将 sW_GUI 实例加载到 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.问题:你提供一个小指南或举一个简单的例子,我必须添加,更改以实现当我想更新 label 内容等时,我不需要将页面加载到框架中。

Many thanks for it.非常感谢。

Hello I have made a small sample:你好我做了一个小样本:

using System;
using System.ComponentModel;
using System.Windows;

namespace My_First_WPF_App
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    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 as SW_GUI;
            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;
            }
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM