Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Pass values between windows

Pass values between windows

Scheduled Pinned Locked Moved WPF
csharpwpfquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shers
    wrote on last edited by
    #1

    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

    E A 2 Replies Last reply
    0
    • S shers

      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

      E Offline
      E Offline
      Elman90
      wrote on last edited by
      #2

      Assuming the values you want to show are contained in Window2. You need to register a method to the "Closed" event of Window2. Like this;

      private void StartWindow()
      {
      Window window2 = new Window();
      window2.Closed += new EventHandler(window2_Closed);
      window2.Show();
      }
      void window2_Closed(object sender, EventArgs e)
      {
      Window window2 = sender as Window;
      if (window2 != null)
      {
      //display message
      MessageBox.Show(window2.Text1);
      MessageBox.Show(window2.Text2);
      }
      }

      1 Reply Last reply
      0
      • S shers

        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

        A Offline
        A Offline
        Andy411
        wrote on last edited by
        #3

        Try it with DataBinding. Just bind the Controls in Window1 and Window2 on the properties of the same object.

        class Data : INotifyPropertyChanged
        {
        private int myVar;

            public int MyProperty
            {
                get { return myVar; }
                set 
                { myVar = value;
                this.OnPropertyChanged("MyProperty");
                }
            }
        
        
        
        
            #region PropertyChangedEvent
            public event PropertyChangedEventHandler PropertyChanged;
        
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        
            #endregion        
        

        }

        And e.g.

        Data d = new Data();
        Window1.DataContext = d;
        Window2.DataContext = d;

        In Windows2.xaml

        e.g. In Windows1.xaml

        <pre lang="HTML">
        <Label Content={Binding MyProperty} />
        </pre>

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups