从TextBox获取文本 [英] Getting text from TextBox

查看:113
本文介绍了从TextBox获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好)
我需要从MVVM应用程序的视图"文本框中获取文本到ModelView.但是我不知道该怎么办.
谢谢)

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)

推荐答案

属性Text有什么问题?如果您到目前为止尚未完成此操作:-),请看这里:
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.text.aspx [ ^ ].

—SA
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


首先,您需要将用作ViewModel的类设置为View的DataContext.

其次,您需要在ViewModel中绑定一个属性.如果要在ViewModel中发生必须在View中显示的更改,则需要继承ViewModel类中的INotifyPropertyChanged.每当您更改属性的值时,都需要使用其属性包含事件名称的事件args引发PropertyChanged事件:

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:

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




第三,您需要在View中进行绑定:

< textbox text ="{Binding PropertyName}">




Third you need to do the binding in the View:

<textbox text="{Binding PropertyName}">


这篇关于从TextBox获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆