保存时文本框中没有文字

时间:2012-05-16 12:02:51

标签: windows-phone-7 mvvm-light

查看:     


TextBox x:Name="feedback" Text="{Binding FeedbackText,Mode=TwoWay}"

视图模型:

public string FeedbackText
{
get
{
        return _feedbackTextProperty;
}

set
{
        _feedbackTextProperty = value;
    RaisePropertyChanged(FeedbackTextPropertyName);
}
}

我正在使用可绑定的应用程序栏但是当我单击该按钮时,FeedbackText属性中没有值。看起来好像“lostfocus”没有触发更新属性。

我正在使用MVVM Light。我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

如果您在单击应用栏按钮时仍然在文本框中有焦点,则文本框将不会触发丢失的焦点事件并导致更新绑定。

是的,这可能令人沮丧。 :(

有各种各样的解决方法,例如在这种情况下强制更新绑定或Coding4Fun Tools中的Binding Helper

答案 1 :(得分:0)

我希望我不会太迟。我在按下ApplicationBarIconButton时使用Window Phone 8保存TextBox文本时遇到了同样的问题。解决此问题的方法是更新焦点TextBox的绑定源属性。您可以使用以下代码执行此操作:

var focusedObject = FocusManager.GetFocusedElement() as TextBox;

if (focusedObject != null)
{
    var binding = focusedObject.GetBindingExpression(TextBox.TextProperty);

    if (binding != null)
    {
        binding.UpdateSource();
    }
}

最佳!

相关问题