Pass values between windows

Page 1 of 1

3 Replies - 6141 Views - Last Post: 03 December 2013 - 02:07 AM Rate Topic: -----

#1 shers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 13

Pass values between windows

Posted 02 December 2013 - 10:25 AM

Hello,

In WPF, I open window2 from window1 with a button click keeping window1 open. When I close window2, I want to display 2 values in window1. How can this be achieved?

Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Pass values between windows

#2 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Pass values between windows

Posted 02 December 2013 - 12:30 PM

There are a lot of ways to achieve this. I have no idea the structure of your program so probably couldn't give the right answer for you. But, in a nutshell, these are possibilities:
  • In an MVVM application, the VM data would change and therefore your windows would update. This is the best way to go with WPF.
  • If you're using code behind, you might add an event to the Window2 which your Window1 could subscribe to, then update its value.
  • Window2 might be a dialog, so you could handle the dialog results.

Was This Post Helpful? 1
  • +
  • -

#3 shers  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-October 13

Re: Pass values between windows

Posted 02 December 2013 - 10:00 PM

Thanks for the reply.

I would choose the second option. However, my window1 is already open. So how do I get the value from window2 when it is closed to the already open window1?

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 MrShoes  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 331
  • View blog
  • Posts: 512
  • Joined: 13-June 12

Re: Pass values between windows

Posted 03 December 2013 - 02:07 AM

I would urge against that option, because it's the WinForms way of doing things. However, if you insisted, I'd do it by first having some custom event arguments to hold the 2 values:

    public class ValueEventArgs : EventArgs
    {
        public object Value1 { get; set; }
        public object Value2 { get; set; }
    }



Next, in Window2, add an event for this.

    public event EventHandler<ValueEventArgs> PassingBackValues;



To raise this event:

    protected void RaisePassingBackValues()
    {
         var handler = PassingBackValues;
         if(handler != null)
             handler(this, new ValueEventArgs{
                 Value1 = TextBoxValue1.Text,
                 Value2 = ComboBoxValue2.SelectedValue
                 });
    }



You need to call that method whenever you want to pass the values to Window1, which sounds like you'd do it in a Closing event handler for Window2. Finally, in Window1, you need to subscribe to the event when it's raised. I'm going to do that when you open the window.

    public void OpenWindow2Button_Click(object sender, RoutedEventArgs e)
    {
        var window2 = new Window2();
        window2.PassingBackValues += (source, args) =>
        {
            LabelDisplayVal1.Content = args.Value1;
            TextBoxDisplayValue2.Text = args.Value2;
        };
        window2.Show();
    }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1