【发布时间】:2014-10-02 22:58:14
【问题描述】:
我正在使用 C#、.NET Framework 4.5.1 和 MVVM 模式开发 WPF。
我有这个TextBox:
<TextBox
x:Name="userName"
HorizontalAlignment="Left"
Height="23"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="231"
Margin="10,10,0,5"
Text="{Binding Path=UserName, Mode=TwoWay}"/>
这是属性:
/// <summary>
/// The <see cref="UserName" /> property's name.
/// </summary>
public const string UserNamePropertyName = "UserName";
private string _userName = null;
/// <summary>
/// Sets and gets the UserName property.
/// Changes to that property's value raise the PropertyChanged event.
/// </summary>
public string UserName
{
get
{
return _userName;
}
set
{
if (_userName == value)
{
return;
}
RaisePropertyChanging(UserNamePropertyName);
_userName = value;
RaisePropertyChanged(UserNamePropertyName);
DoLoginCommand.RaiseCanExecuteChanged();
}
}
我的问题是在TextBox 失去焦点之前我无法获得新值。
当用户在TextBox 上输入字符时,有什么方法可以通知ViewModel?
【问题讨论】:
-
查看 Binding 文档所花费的时间比编写此问题所花费的时间要少。
标签: c# wpf mvvm mvvm-light