UWP深入学习三:依赖属性、附加属性和数据绑定

Dependency properties overview

Custom dependency properties

Attached properties overview

Custom attached properties

Quickstart: Data binding to controls

Data binding overview (XAML)

使用代码创建绑定

你还可以使用规程代码而不是 XAML 来将 UI 元素连接到数据。若要执行此操作,先创建新 Binding 对象,设置适当的属性,然后调用 FrameworkElement.SetBindingBindingOperations.SetBinding。如果你希望在运行时选择绑定属性值或在多个控件中共享单个绑定,以编程方式创建绑定十分有用。但是请注意,调用 SetBinding 后,无法更改绑定属性值。

以下示例演示了如何使用代码实现之前的绑定。

// Create an instance of the MyColors class 
// that implements INotifyPropertyChanged.
MyColors textcolor = new MyColors();

// Brush1 is set to be a SolidColorBrush with the value Red.
textcolor.Brush1 = new SolidColorBrush(Colors.Red);

// Set the DataContext of the TextBox MyTextBox.
MyTextBox.DataContext = textcolor;

// Create the binding and associate it with the text box.
Binding binding = new Binding() { Path = new PropertyPath("Brush1") };
MyTextBox.SetBinding(TextBox.ForegroundProperty, binding);

以增量方式加载数据

你可以使用增量加载将列表控件绑定到任意的大型数据源,且仍获得高性能。例如,你可以将列表控件绑定到必应图像查询结果,而无需一次性加载所有结果。你只需立即加载部分结果,再根据需要加载其他结果。 为支持增量加载,你必须在支持集合更改通知的数据源上实现 ISupportIncrementalLoading。当数据绑定引擎请求更多数据时,你的数据源必须发出相应的请求、集成结果,然后发送相应的通知以更新 UI。有关详细信息,请参阅 XAML 数据绑定示例

How to bind to hierarchical data and create a master/details view (XAML)

Quickstart: Reading and writing files

Quickstart: Accessing files with file pickers

How to continue your Windows Phone app after calling a file picker

原文地址:https://www.cnblogs.com/qianblue/p/5033495.html