Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello)
I need to get text from the textbox in View to ModelView in my MVVM app. But I don't know how to do it.
Thank you)
Posted
Comments
Sergey Alexandrovich Kryukov 11-May-12 11:36am    
How is it related to MVVM at all? What's wrong with MSDN information?
--SA
asinefa 11-May-12 12:30pm    
directly. I need to take for creating some object. But i haven't any access to it from ModelView

What's wrong with the property Text? If you haven't done this so far :-), look here:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text.aspx[^].

—SA
 
Share this answer
 
Comments
VJ Reddy 11-May-12 11:50am    
My 5!
Sergey Alexandrovich Kryukov 11-May-12 11:50am    
Thank you, VJ.
--SA
Dylan Morley 11-May-12 11:53am    
Easiest 5 you'll ever earn!
Sergey Alexandrovich Kryukov 11-May-12 13:56pm    
Thank you, Dylan.
I thought about doing it a bit harder, but it turned out to be too hard to do... :-)
--SA
Maciej Los 11-May-12 12:11pm    
;) 5+
First you need to have the class being used as the ViewModel set as the DataContext for the View.

Second you need a property in the ViewModel that will be bound to. If there is going to be changes in the ViewModel that have to appear in the View, then need to inherit INotifyPropertyChanged in the ViewModel class. Whenever you change the value of the property you need to raise the PropertyChanged event with its event args containing the name of the property:

C#
public string PropertyName
{
    get { return propertyName; }
    set
    {
        propertyName= value;
        if (PropertyChanged != null)
           PropertyChanged(this, new PropertyChangedEventArgs("PersonName");
    }
}



Third you need to do the binding in the View:

<textbox text="{Binding PropertyName}">
 
Share this answer
 
Comments
asinefa 12-May-12 7:28am    
Thanks) it's really so so easy
Clifford Nelson 12-May-12 12:40pm    
If you liked my answer should have rated it high and if you think it answered your question well, mark it as answering the question (there can be more than one solutions).

Glad I was of help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900