鸿 网 互 联 www.68idc.cn

当前位置 : 服务器租用 > .net技术 > WinRT/Metro > >

绑定: 与 Element Model Indexer Style RelativeSource 绑定,

来源:互联网 作者:佚名 时间:2013-10-28 10:20
重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换Posted on [源码下载] 重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定

重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换 Posted on

[源码下载]


重新想象 Windows 8 Store Apps (52) - 绑定: 与 Element Model Indexer Style RelativeSource 绑定, 以及绑定中的数据转换



作者:webabcd


介绍
重新想象 Windows 8 Store Apps 之 绑定



示例
1、演示如何与 Element 绑定,以及 OneTime, OneWay, TwoWay 的区别
Binding/BindingElement.xaml

<Page x:Class="XamlDemo.Binding.BindingElement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Binding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable 本例用于演示如何与 Element 绑定,以及 OneTime, OneWay, TwoWay 的区别 --> <!-- OneTime 方式绑定元素 OneWay 方式绑定元素(OneWay 是默认方式) TwoWay 方式绑定元素


2、演示如何与 Model 进行双向绑定
Binding/BindingModel.xaml

<Page x:Class="XamlDemo.Binding.BindingModel" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Binding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable

Binding/BindingModel.xaml.cs

/* * 演示如何与 Model 进行双向绑定 */ using System; using System.ComponentModel; using Windows.System.Threading; using Windows.UI.Core; using Windows.UI.Xaml.Controls; using XamlDemo.Model; namespace XamlDemo.Binding { BindingModel : Page { private Employee _employee; public BindingModel() { this.InitializeComponent(); this.Loaded += BindingModel_Loaded; } void BindingModel_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // 创建一个需要绑定的实体对象(注:Employee 实现了 INotifyPropertyChanged 接口,想要 OneWay 或者 TwoWay 的话必须要实现 INotifyPropertyChanged 接口) _employee = new Employee(); _employee.Name = ; _employee.Age = 33; _employee.IsMale = true; // 每 5 秒更新一次数据 ThreadPoolTimer.CreatePeriodicTimer( (timer) => { var ignored = Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { Random random = new Random(); _employee.Age = random.Next(10, 100); _employee.IsMale = random.Next() % 2 == 0 ? true : false; }); }, TimeSpan.FromMilliseconds(5000)); // Employee 对象的属性的值发生变化时触发的事件 _employee.PropertyChanged += _employee_PropertyChanged; root.DataContext = _employee; } _employee_PropertyChanged(object sender, PropertyChangedEventArgs e) { lblMsg.Text = + e.PropertyName + ; lblMsg.Text += Environment.NewLine; lblMsg.Text += , _employee.Name, _employee.Age, _employee.IsMale); } } }


3、演示如何与索引器进行绑定
Binding/BindingIndexer.xaml

<Page x:Class="XamlDemo.Binding.BindingIndexer" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Binding" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable

Binding/BindingIndexer.xaml.cs

/* * 演示如何与索引器进行绑定 */ using System.Collections.Generic; using Windows.UI.Xaml.Controls; using XamlDemo.Model; namespace XamlDemo.Binding { BindingIndexer : Page { public BindingIndexer() { this.InitializeComponent(); this.Loaded += BindingIndexer_Loaded; } void BindingIndexer_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // 用于演示如何绑定集合中的某个元素 List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add(+ i.ToString()); } textBlock.DataContext = list; // 用于演示如何绑定集合中的某个对象的某个属性 textBlock2.DataContext = TestData.GetEmployees(); // 用于演示如何绑定 string 类型的索引器 textBlock3.DataContext = this; // 用于演示如何绑定字典表中指定 key 的数据 Dictionary<string, string> dic = new Dictionary<string, string>(); dic[] = ; textBlock4.DataContext = dic; } [string indexer] { get { return indexer; } } } }


4、演示 Style 中的 Setter 如何做数据绑定(绑定静态资源)
Binding/BindingStyleSetter.xaml

网友评论
<