WINRT-based Windows Phone 8.1 and the XAML data binding in Windows 8.1 add some new features.
Fallbackvalue Property : Fallbackvalue You can specify a substitute property and value display when the value of the bound Value property does not exist.
1: <TextBlockText
2: fallbackvalue= ' This is a fallback value '} "
3: Grid. Column= "1"></TextBlock>
The Fallbackvalue attribute is set in the preceding code, and it is said that when the Badparh property value does not exist (no return value), the "This is a fallback value" text is substituted for display.
For a detailed explanation of the Fallbackvalue property, refer to MSDN:
Http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/windows.ui.xaml.data.binding.fallbackvalue.aspx
Targetnullvalue Property : Targetnullvalue You can specify a substitute property or value display when the bound property value returns NULL.
1: <ButtonContent
2: />
The Targetnullvalue property value is set in the preceding code, which means that when the Nextitem value of the Button.content property binding is null, the value of the Nullvalue property is used for display.
For a detailed explanation of the Targetnullvalue property, refer to MSDN:
Http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/windows.ui.xaml.data.binding.targetnullvalue.aspx
UpdateSourceTrigger Property : Determines the execution time of the source update for two-way bound bindings by setting the UpdateSourceTrigger property value.
In fact, the UpdateSourceTrigger property has already appeared in WPF and Silverlight, and in Windows Phone 8.1 and in Windows 8.1, this property was introduced in XAML.
1: <TextBoxx:name= "NameTxtBox1"Text= "{ Binding Name, Mode=twoway, Updatesourcetrigger=default} "Width=" HorizontalAlignment= "Left"Grid. Column = "1" Grid. Row = "1" />
2: < textbox x:name = "Nametxtbox" text = "{Binding Name, Mode=twoway, updatesourcetrigger=explicit}" width = "$" grid . row = "2" horizontalalignment = "left" grid . column = "1" />
3: < textbox Span class= "attr" >x:name = "NameTxtBox3" text = "{Binding Name, Mode=twoway, updatesourcetrigger=propertychanged}" width = "$" grid . row = "3" horizontalalignment = "left" grid . column = "1" />
As you can see from the code above, UpdateSourceTrigger contains 3 enumeration values.
Default: Defaults behavior in the use of dependency properties (which use bindings). When WINRT is running, the effect of implementing a data-binding update is equivalent to propertychanged.
PropertyChanged: The binding source is updated whenever the binding target value changes. This is automatically detected by the binding system.
Explicit: The binding source is updated only when the Bindingexpression.updatesource method is called.
The popular point of understanding is that when the UpdateSourceTrigger property value is set to default or PropertyChanged, when the target property value is updated, the data source is updated immediately, and when set to explicit, You need to get bindingexpression from an object using the Getbindingexpression method, where the binding of the dependency property exists in that object. Then call the Get Updatesource method of the BindingExpression object to update the data source, the code can refer to the following:
1: var expression = nametxtbox.getbindingexpression (Textbox.textproperty);
2: expression. Updatesource ();
For a detailed explanation of the UpdateSourceTrigger property, refer to MSDN:
Http://msdn.microsoft.com/zh-cn/library/windows/apps/xaml/windows.ui.xaml.data.binding.updatesourcetrigger.aspx
In addition, the ViewModel with attribute notification in the inheritance INotifyPropertyChanged can be optimized using the new features in the c#5.0.
ViewModel implementations that are not optimized:
Note the red box, if one day the ID property name changes in the project, there is a great chance that the notifypropertychanged ("id") in the "id" text is forgotten to update, which will result in a two-way binding property value change notification can not be implemented.
So we can optimize the above code:
In the above code, optimizations are primarily implemented by the Callermembername feature. Callermembername is a feature of the caller information introduced in c#5.0, which makes it easy to get the caller's function name and location through the caller information .
For detailed explanation of caller information, refer to MSDN:
http://msdn.microsoft.com/zh-cn/library/vstudio/hh534540 (v=vs.110). aspx
Building WINRT-based WP8.1 App 02: New features for data binding