简体   繁体   中英

C# Passing data Between windows(WPF)

Recently picked up C# in university, trying to work out how to pass the variable "name" in MainWindow.xaml to ThirdWindow.xaml?

The below code is for the main window where the data is assigned to the variable "name"

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

    public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        string name = NameBox.Text;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SecondWindow newWin = new SecondWindow();
        newWin.Show();
        this.Close();
    }
}

The below code is for the third window

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

    public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LstThanks.Items.Add(name);
    }
}

You can simply pass that string name variable in the constructor as an argument to the ThirdWindow on Button_Click event.

   private void Button_Click(object sender, RoutedEventArgs e)
    {
        var name = "your name";
        var newWin = new ThirdWindow(name);
        newWin.Show();
        this.Close();
    }

That string text will be available in the constructor of ThirdWindow .

public partial class ThirdWindow: Window
{
    public ThirdWindow(string name)
    {
        InitializeComponent();      
    }

    public void LstThanks_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LstThanks.Items.Add(name);
    }
}

You can pass the variable through the constructor of the new window

var win = new ThirdWindow(name);

public ThirdWindow(string name)
{
    InitializeComponent();      
}

Another method is to pass it through an event message. This will require you to write a new message and add an event listener to your constructor in the ThirdWindow class. If you google this there are a variety of examples out there on how to do such a thing.

Here you are defining a local variable name. This variable is visible only inside the {} block. So we cannot use it anywhere else.

public void NameBox_TextChanged(object sender, TextChangedEventArgs e)
{
    string name = NameBox.Text;
}

You could add a new string property into second window and pass value through that to all the way into third form.

So, add new property into your two windows (SecondWindow, ThirdWindow)

public string Name { get; set; }

These properties are holding the data for whole their lifetime (until they are closed).

Remove NameBox_TextChanged event handling, because we don't need it. Add property setting inside your buttons click event

private void Button_Click(object sender, RoutedEventArgs e)
{
    SecondWindow newWin = new SecondWindow();
    newWin.Name = NameBox.Text; //Store value into SecondWindow variable
    newWin.Show();
    this.Close();
}

Now when SecondWindow is visible (Show is called), you have name value available in Name variable and you should be able to copy this behavior for ThirdWindow.

If the ThirdWindow window is dependent on the name value then you can pass it through the constructor:

public partial class ThirdWindow : Window
{
    public string Name { get; set; }

    public ThirdWindow(string name)
    {
        InitializeComponent();   
        Name = name;
    }
}

or if not then make a method on the ThridWindow to set the name :

public partial class ThirdWindow : Window
{
    public string Name { get; set; }

    public void SetName(string name)
    {  
        Name = name;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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