TextBox绑定TwoWay不会更新,直到焦点丢失WP7

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)

  • 谢谢.很高兴知道原因. (3认同)

Pra*_*ian 16

下载Charles Petzold的免费书籍Windows Phone 7.在页387他谈到如何做到这一点.

基本上,设置to 的UpdateSourceTrigger属性.然后,在的回调,更新绑定源.BindingExplicitTextBoxTextChanged


Der*_*kin 8

您可以使用Prism Library for WP7中UpdateTextBindingOnPropertyChanged行为来更新文本更改时的绑定值,而不是丢失焦点.

  • 我已经在使用MVVM Light,并且不想仅仅为此包含Prism.:( (4认同)

Raj*_*jiv 8

尝试将UpdateSourceTrigger属性设置为'PropertyChanged'

像这样

Property="{Binding PropertyBinding, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)


rrh*_*tjr 7

我正朝着@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失去焦点时单次"刷新" ).

希望有所帮助!

  • 将Text属性设置为Text ="{Binding TextViewModelProperty,Mode = TwoWay,UpdateSourceTrigger = PropertyChanged}"为我工作(我正在使用SL5). (4认同)
  • Silverlight不支持"UpdateSourceTrigger"的"PropertyChanged"值.您已链接WPF主题,该主题不适用于WP7. (3认同)

Ton*_*all 6

以下是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项目).

现在该如何使用它:

  1. 将System.Windows.Interactivity程序集的引用添加到Windows Phone项目.
  2. 在要使用该行为的每个页面中,向程序集添加XAML引用:xmlns:i ="clr-namespace:System.Windows.Interactivity; assembly = System.Windows.Interactivity"
  3. 在要应用bahvior(已经具有与源属性的TwoWay绑定)的每个TextBox的XAML内,添加以下内容:

    <i:Interaction.Behaviors>
    <my:UpdateTextBindingOnPropertyChanged />
    </i:Interaction.Behaviors>

    注意:代码中的"my:"前缀可能有所不同.它只是您添加行为类的命名空间引用.