Bindings have three binding patterns, and bindings also divide UI-to-UI bindings and custom data source-to-UI bindings.
The binding of the custom data source to the UI is more complex. If we use the data context DataContext to bind the data, when we change the data source
Data, it is found that the corresponding data on the binding target UI has not changed, which is supposed to be the default binding OneWay mode, the change of the data source
Should cause changes to the target properties of the interface UI.
What is this for? because the specific data source properties do not implement change notification, the data source data changes but cannot be notified to the target UI, popular point to
That is, the data source of your own changes, you do not notice me as the UI, I know where you changed, I do not know that I do not change it.
Of course a problem arises and there is always a solution. The problem that needs to be solved now is how to detect the change of data source, must give the data source implement a
The appropriate property change notification mechanism.
Solution: The data source must implement the INotifyPropertyChanged interface, which has a propertychanged event that notifies the binding
The engine source has changed so that the binding engine can update the target value. If you want to implement this interface, you need to declare the PropertyChanged event and create
OnPropertyChanged method. For each property that needs to change the notification, you need to call onpropertychanged whenever an update is made
Method.
The sample code is as follows:
XAML Code:
<page x:class= "App1.databinddemo" xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local= "Using:app1" xmlns:d= "http// schemas.microsoft.com/expression/blend/2008 " xmlns:mc=" http://schemas.openxmlformats.org/ markup-compatibility/2006 " mc:ignorable=" D " background=" {ThemeResource Applicationpagebackgroundthemebrush} "> <Grid> <textblock text=" {Binding Test} "fontsize=" 25 " horizontalalignment= "center"/> <button x:name= "Change" horizontalalignment= "center" width= "Content" = "Change data source data" click= "Change_click"/> </Grid></Page>
. CS Code:
Using system;using system.collections.generic;using system.componentmodel;using system.io;using System.Linq;using System.runtime.interopservices.windowsruntime;using windows.foundation;using Windows.Foundation.Collections; Using windows.ui.xaml;using windows.ui.xaml.controls;using windows.ui.xaml.controls.primitives;using windows.ui.xaml.data;using windows.ui.xaml.input;using windows.ui.xaml.media;using Windows.UI.Xaml.Navigation;//" Blank page "Item template in http://go.microsoft.com/fwlink/? linkid=390556 on the namespace app1{////<summary>//////for self or to navigate to a blank page inside the Frame. </summary> public sealed partial class Databinddemo:page {public class TESTDATA:INOTIFYPROPERTYC Hanged {private string test; public string Test {get {return Test;} set {test = value; OnPropertyChanged ("Test"); }} public Event PropertychangEdeventhandler propertychanged; protected void OnPropertyChanged (string name) {PropertyChangedEventHandler handler = Propertych anged; if (handler!=null) {handler (this, new PropertyChangedEventArgs (name)); }}} TestData TestData = new TestData () {test= "See, this is DataContext bound data! "}; Public Databinddemo () {this. InitializeComponent (); This. DataContext = TestData; }///<summary>///This page will be called when it is displayed in Frame. </summary>//<param name= "E" > Describes how to access event data for this page. This parameter is typically used for configuration pages. </param> protected override void Onnavigatedto (NavigationEventArgs e) {} private void C Hange_click (object sender, RoutedEventArgs e) {testdata.test = "Look, this is the new binding data! "; } }}
Data binding in Windows Phone 8.1 (solution with no change in binding data)