【wpf】Bingding的方向和触发的时机
<!--两个方向都可行-->
<TextBox Text="{Binding DM1.Value,UpdateSourceTrigger=PropertyChanged,
Mode=TwoWay}" Name="textBox"/>
<!--只允许从源到目标-->
<TextBox Text="{Binding DM1.Value,Mode=OneWay}" Name="tb2"/>
<!--史允许从目标到源-->
<TextBox Text="{Binding DM1.Value,UpdateSourceTrigger=PropertyChanged,
Mode=OneWayToSource}" />
<!--只允许初始化一次-->
<TextBox Text="{Binding DM1.Value,
Mode=OneTime}"/>
Mode
首先源是指后台数据,目标指,前台显示。
TwoWay:"双向奔赴"。
OneWay 后台数据 -> 前台显示,
OneWayToSource 与OneWay 相反。
OneTime 就只有初始化的时候起作用了。
UpdateSourceTrigger
UpdateSourceTrigger ,表示当前台数据(目标)发送变化时,更新源(后台数据)的时机。(注意:UpdateSourceTrigger 关心的方向是:前台显示->后台数据,所以当Mode为OneWay时,设置这个属性是没有意义的)
//
// 摘要:
// Describes the timing of binding source updates.
public enum UpdateSourceTrigger
{
//
// 摘要:
// The default System.Windows.Data.UpdateSourceTrigger value of the binding target
// property. The default value for most dependency properties is System.Windows.Data.UpdateSourceTrigger.PropertyChanged,
// while the System.Windows.Controls.TextBox.Text property has a default value of
// System.Windows.Data.UpdateSourceTrigger.LostFocus.
Default = 0,
//
// 摘要:
// Updates the binding source immediately whenever the binding target property changes.
PropertyChanged = 1,
//
// 摘要:
// Updates the binding source whenever the binding target element loses focus.
LostFocus = 2,
//
// 摘要:
// Updates the binding source only when you call the System.Windows.Data.BindingExpression.UpdateSource
// method.
Explicit = 3
}
PropertyChanged
表示,只要前台发送变化,立马通知源(也就是调用属性的set方法)
LostFocus
表示控件失去焦点时触发。
Explicit
通过绑定表达式调用UpdateSource调用后触发。
//获取绑定表达式
BindingExpression bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
//调用后触发
bindingExpression.UpdateSource();
作者:宋桓公
出处:http://www.cnblogs.com/douzi2/
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现