locked
sending data from one window textbox's text to another window having textbox in MVVM RRS feed

  • Question

  • can you please suggest me any good example of passing the textbox value of one window to another windows textbox in MVVM structure..

    Tuesday, July 23, 2013 10:03 AM

Answers

  • Ok here is a scenario; I have a window called MembersWindow and another one called MemberDetailsWindow. When a member name is entered in a textbox called nameTextBox from MembersWindow, I want to open MemberDetailsWindow with the data that was entered in the textbox from MembersWindow and put that data in a textbox called userNameTextBox on the MemberDetailsWindow window.

    Here is how I will solve it. In the MembersWindow class, I will handle the event that opens MemberDetailsWindow and in the event handler, I will write something like this:

    MemberDetailsWindow memberDetailsWindow = new MemberDetailsWindow (nameTextBox.Text.Trim());
    memberWindow.Show(); //or ShowDialog(), whichever works for you.

    Then in the MemberDetailsWindow class, I will write an overload of the constructor that accepts a string and then use that to assign the value to a field:

    string _dataFromOtherWindow; public MemberDetailsWindow(string data) : this()//you have to call the default constructor { this._dataFromOtherWindow = data; }

    Then wherever I need the data, I will put it in the usernameTextBox like I will any other data.

    Hope this helps.

    If this helped, please mark ad answer.



    The best things in life are free, but the most valuable ones are costly...use opportunities well for there are others like you who deserves them, but don't have them...


    • Edited by talk2alie Tuesday, July 23, 2013 6:42 PM Error
    • Proposed as answer by talk2alie Wednesday, July 24, 2013 3:13 PM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:30 AM
    Tuesday, July 23, 2013 6:40 PM

All replies

  • Ok here is a scenario; I have a window called MembersWindow and another one called MemberDetailsWindow. When a member name is entered in a textbox called nameTextBox from MembersWindow, I want to open MemberDetailsWindow with the data that was entered in the textbox from MembersWindow and put that data in a textbox called userNameTextBox on the MemberDetailsWindow window.

    Here is how I will solve it. In the MembersWindow class, I will handle the event that opens MemberDetailsWindow and in the event handler, I will write something like this:

    MemberDetailsWindow memberDetailsWindow = new MemberDetailsWindow (nameTextBox.Text.Trim());
    memberWindow.Show(); //or ShowDialog(), whichever works for you.

    Then in the MemberDetailsWindow class, I will write an overload of the constructor that accepts a string and then use that to assign the value to a field:

    string _dataFromOtherWindow; public MemberDetailsWindow(string data) : this()//you have to call the default constructor { this._dataFromOtherWindow = data; }

    Then wherever I need the data, I will put it in the usernameTextBox like I will any other data.

    Hope this helps.

    If this helped, please mark ad answer.



    The best things in life are free, but the most valuable ones are costly...use opportunities well for there are others like you who deserves them, but don't have them...


    • Edited by talk2alie Tuesday, July 23, 2013 6:42 PM Error
    • Proposed as answer by talk2alie Wednesday, July 24, 2013 3:13 PM
    • Marked as answer by Lisa Zhu Saturday, July 27, 2013 3:30 AM
    Tuesday, July 23, 2013 6:40 PM
  • hi talk2alie ,thanks for the reply.Your answer is correct.I knew this way of sending  textbox1 text of window1 to window2's textbox2 earlier.

    In my case,started learning to implement the MVVM structure in WPF application from past one week only.I have gone through a simple MVVM structure and implemented the same.So now i want to implement MVVM structure for my actual application.

    User.cs -contains  FirstName,LastName,Title,street,city,state,pincode properties

    UserModelview.cs contains  street,city,state,pincode properties as well as PropertyChanged Event 

    Checkaddress.xaml -conatins street,city,state,pincode textboxes and two buttons-Ok and cancel.

    MainWindow.xaml -contains only one textbox1.

    if i entered the text address in textbox1 on Lost FocusEvent it will open  Checkaddress.xaml window with text that contains textbox1.text.

    Suppose if i fill all the data in Checkaddress.xaml window on button ok click event ,it should reflect all the texboxes data as full address and appear in textbox1.text. 

    My question is , can i use the same way that you mentioned above for MVVM structure also? because binding User.cs property value in xaml text binding. Please do reply even if you feel silly question to answer.

    Thanks in advance.


    • Edited by kida.altantis Monday, July 29, 2013 11:19 AM typing mistake
    Monday, July 29, 2013 11:18 AM