Jos*_*ose 43 data-binding silverlight xaml windows-phone-7
我有一个页面,其中包含一些用于数据输入的文本框.文本框的绑定设置为TwoWay.如果文本框失去焦点,则视图模型中的数据仅会更新.如果单击某个按钮(例如"保存"),并且文本框仍具有焦点,则在保存事件的视图模型中不会更改文本框中的更改.
有没有办法让绑定在失去焦点之前保存文本框的值?或者我是否需要在保存事件中执行某些操作?
Ste*_*SFT 58
我假设您的保存按钮是ApplicationBarButton(不是普通按钮).对于普通按钮,它只会起作用,因为它们会聚焦,因此数据绑定会起作用.
对于手机上的ApplicationBarButtons,它们有点不同,因为它们不会将焦点从客户端应用程序中移开.要确保在单击"保存"按钮时启动数据绑定,可以在处理程序中添加以下代码:
object focusObj = FocusManager.GetFocusedElement();
if (focusObj != null && focusObj is TextBox)
{
var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);
binding.UpdateSource();
}
Run Code Online (Sandbox Code Playgroud)
Pra*_*ian 16
下载Charles Petzold的免费书籍Windows Phone 7.在页387他谈到如何做到这一点.
基本上,设置to 的UpdateSourceTrigger属性.然后,在的回调,更新绑定源.BindingExplicitTextBoxTextChanged
您可以使用Prism Library for WP7中的UpdateTextBindingOnPropertyChanged行为来更新文本更改时的绑定值,而不是丢失焦点.
尝试将UpdateSourceTrigger属性设置为'PropertyChanged'
像这样
Property="{Binding PropertyBinding, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)
我正朝着@Praetorian的方向前进.
您TextBox的默认UpdateSourceTrigger值为LostFocus.这意味着该值仅在...失去焦点时被推送到ViewModel属性.
您可以将UpdateSourceTrigger设置为PropertyChanged:
<TextBox UpdateSourceTrigger="PropertyChanged" Text="{Binding TextViewModelProperty}" />
Run Code Online (Sandbox Code Playgroud)
来自http://msdn.microsoft.com/en-us/library/system.windows.data.binding.updatesourcetrigger.aspx:
UpdateSourceTrigger值之一.默认值为Default,它返回目标依赖项属性的默认UpdateSourceTrigger值.但是,大多数依赖项属性的默认值是PropertyChanged,而Text属性的默认值是LostFocus.
请记住,这意味着更新此属性触发的任何事情都会更频繁地发生(基本上是每次按键时,而不是在TextBox失去焦点时单次"刷新" ).
希望有所帮助!
以下是Derek建议的Microsoft解决方案的快速访问答案.而不是下载和筛选所有Prism的东西,只需将此类复制到您的项目中,然后按照以下步骤激活它:
UpdateBindingOnPropertyChangedBehviour.cs
using System;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Interactivity;
namespace MyCompany.MyProduct
{
/// <summary>
/// Custom behavior that updates the source of a binding on a text box as the text changes.
/// </summary>
public class UpdateTextBindingOnPropertyChanged : Behavior<TextBox>
{
/// <summary>
/// Binding expression this behavior is attached to.
/// </summary>
private BindingExpression _expression;
/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
protected override void OnAttached()
{
base.OnAttached();
// Hook events to change behavior
_expression = AssociatedObject.GetBindingExpression(TextBox.TextProperty);
AssociatedObject.TextChanged += OnTextChanged;
}
/// <summary>
/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
/// </summary>
/// <remarks>
/// Override this to unhook functionality from the AssociatedObject.
/// </remarks>
protected override void OnDetaching()
{
base.OnDetaching();
// Un-hook events
AssociatedObject.TextChanged -= OnTextChanged;
_expression = null;
}
/// <summary>
/// Updates the source property when the text is changed.
/// </summary>
private void OnTextChanged(object sender, EventArgs args)
{
_expression.UpdateSource();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这基本上是Microsoft Prism 4.1代码的清理版本(如果您想浏览其余部分,请参阅Silverlight\Prism.Interactivity项目).
现在该如何使用它:
在要应用bahvior(已经具有与源属性的TwoWay绑定)的每个TextBox的XAML内,添加以下内容:
<i:Interaction.Behaviors>
<my:UpdateTextBindingOnPropertyChanged />
</i:Interaction.Behaviors>
注意:代码中的"my:"前缀可能有所不同.它只是您添加行为类的命名空间引用.
| 归档时间: |
|
| 查看次数: |
24864 次 |
| 最近记录: |