gpt4 book ai didi

c# - 当用户在文本框中输入字符时通知 ViewModel

转载 作者:行者123 更新时间:2023-12-03 10:55:07 25 4
gpt4 key购买 nike

我正在使用 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}"/>

这是属性(property):
/// <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 才能获得新值失去焦点。

有什么方法可以通知 ViewModel当用户在 TextBox 上键入字符时?

最佳答案

在您的绑定(bind)中,指定 UpdateSourceTrigger=PropertyChanged

<TextBox 
x:Name="userName"
HorizontalAlignment="Left"
Height="23"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="231"
Margin="10,10,0,5"
Text="{Binding Path=UserName, UpdateSourceTrigger=PropertyChanged}"/>

关于c# - 当用户在文本框中输入字符时通知 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26163554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com