C# WPF 中的文本框绑定更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12019737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:05:54 来源:igfitidea点击:
Textbox binding update in WPF
提问by OneMoreVladimir
MessageText property gets updated only when I hit another control. What is more if I press any button it's Click handler isn't executed and the MessageText set is executed instead. I've broken my head.
MessageText 属性仅在我点击另一个控件时更新。更重要的是,如果我按下任何按钮,则不会执行 Click 处理程序,而是执行 MessageText 集。我摔断了头。
<TextBox x:Name="messageText" Grid.Row="1" Grid.Column="0"
TextWrapping="Wrap" Text="{Binding Path=MessageText, Mode=TwoWay}"/>
private void ChatView_Loaded(object sender, RoutedEventArgs e)
{
DataContext = viewModel;
}
public string MessageText
{
get
{
return this.messageText;
}
set
{
this.messageText = value;
OnProperyChanged("MessageText");
}
}
采纳答案by Aghilas Yakoub
You can adjust UpdateSourceTrigger to PropertyChanged
您可以将 UpdateSourceTrigger 调整为 PropertyChanged
<TextBox x:Name="messageText" Grid.Row="1" Grid.Column="0"
TextWrapping="Wrap" Text="{Binding Path=MessageText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

