WPF数据绑定后点击保存按钮 [英] WPF databinding after Save button click

查看:126
本文介绍了WPF数据绑定后点击保存按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序和一个设置窗口与TabControl包含几个TabItems。每个都有一些字段(文本框),它们被数据绑定到同一个Singleton对象。

在保存按钮单击之后,是否有任何优雅和类似WPF的数据绑定方式?

现在,在更改文本框的内容后立即进行数据绑定,我希望该单例具有旧值,并在点击保存按钮后更新它们。

解决方案

对于 Textbox 在XAML中使用的 DataBinding 对象,请使用 code> UpdateSourceTrigger 属性,值为显式如下:

 < TextBox Name =itemNameTextBox
Text ={Binding Path = ItemName,UpdateSourceTrigger = Explicit}/>

将UpdateSourceTrigger值设置为Explicit时,源值仅在应用程序调用 UpdateSource 方法如下(您可以将以下代码放在Save 中单击事件):

  BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty); 
be.UpdateSource();


I have an app and a Settings window with TabControl containing couple of TabItems. Each of them have some fields (textboxes) which are databinded to the same Singleton object.
Is there any elegant and WPF-like way to the the databinding only after Save button click?
Right now it's databinded immediately after changing the content of the textbox, and I want that singleton have old values and update them only after clicking the save button.

解决方案

For your DataBinding object used in XAML for the Textbox, use the UpdateSourceTrigger property with value Explicit as below:

<TextBox Name="itemNameTextBox"
     Text="{Binding Path=ItemName, UpdateSourceTrigger=Explicit}" />

When you set the UpdateSourceTrigger value to Explicit, the source value only changes when the application calls the UpdateSource method as below (you can put below code in Save Click event):

BindingExpression be = itemNameTextBox.GetBindingExpression(TextBox.TextProperty);
be.UpdateSource();

这篇关于WPF数据绑定后点击保存按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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