“UpdateSourceTrigger=PropertyChanged” equivalent f

2019-01-22 22:34发布

问题:

In WPF, we can update the underlying data model whenever the user makes any change to the data by leveraging UpdateSourceTrigger like this:

<TextBox Text="{Binding Path=TextProperty, UpdateSourceTrigger=PropertyChanged}"/>

In Window Phone UpdateSourceTrigger was not included in the XAML specification and to accomplish the same, a TextChanged handler was necessary like this:

(sender as TextBox).GetBindingExpression(TextBox.TextProperty).UpdateSource();

In Windows 8, I assumed that UpdateSourceTrigger would also be omitted - I was correct. I was surprised, however, to learn that GetBindingExpression() was also missing.

What is the best technique to accomplish, in Windows 8, what we accomplished in WPF with UpdateSourceTrigger and in Windows Phone with GetBindingExpression().UpdateSource()?

Please note: This question is for Windows 8 (Metro) development; this other question ( "UpdateSourceTrigger=PropertyChanged" equivalent for a Windows Phone 7 TextBox ) is for Windows Phone development - not to be confused.

As a matter of research, this example (which I created) uses a TextBox override to swap the text value between two properties causing the update. It is based on this. But, is there a better way? Something elegant?

回答1:

It seems there is no GetBindingExpression or equivalent workaround. However you can extend the TextBox like this: https://mytoolkit.svn.codeplex.com/svn/WinRT/Controls/ExtendedTextBox.cs

Still this doesn't solve the problem itself... (it's really sad)

See also:

  • http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/a04dc907-9ca8-4302-bbad-c00b01b8193f/
  • http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/69214cce-787b-40c6-9d40-a556c5636119/
  • http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/775f1692-2837-471c-95fc-710bf0e9cc53


回答2:

It seems UpdateSourceTrigger and GetBindingExpression are available in Windows 8.1. Thanks to Rico Suter above and HDW Production from this question:

Windows Store TextBox - How to update binding on Enter key-up?



回答3:

Jerry,

The pattern that you should use would be implementing INotifyPropertyChanged. Heres and example from MSDN: http://msdn.microsoft.com/en-us/library/ms229614.aspx

In this way, any XAML object that is bound to a backing property or field, will be notified when the backing field's value has changed, because of the call to the NotifyPropertyChanged() method.

If you were to implement this pattern you wouldnt need to explicitly define when to update the UI, all UI elelments that are bound to Notifyable propertied will as you're expecting Update when the Source Changes.

For Classes that Implement INotifyPropertyChanged

I use a snippet to create most of my properties like this (if there is any chance they will be bound or will need to notify another object):

private PropertyChangedEventArgs myVarChangedEventArgs = new PropertyChangedEventArgs("MyProperty");
private int myVar;
public int MyProperty
{
get { return myVar; }
set
  {
     if (myVar != value)
     {
        myVar = value;
        NotifyPropertyChanged(myVarChangedEventArgs);
      }
   }
}