背水一战 Windows 10 之 绑定

  • 通过 Binding 绑定对象
  • 通过 x:Bind 绑定对象
  • 通过 Binding 绑定集合
  • 通过 x:Bind 绑定集合

示例
1、演示如何通过 Binding 绑定对象
Bind/BindingModel.xaml

<Page
x:Class="Windows10.Bind.BindingModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="root" Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <TextBox Text="{Binding Name, Mode=TwoWay}" Margin="5" />
<TextBox Text="{Binding Age, Mode=TwoWay}" Margin="5" />
<ToggleSwitch IsOn="{Binding IsMale, Mode=TwoWay}" OffContent="女" OnContent="男" Header="性别" Margin="5" /> </StackPanel>
</Grid>
</Page>

Bind/BindingModel.xaml.cs

/*
* 演示如何通过 Binding 绑定对象
*
*
* 如果需要数据源在属性值发生变化时对外通知,则需要实现 INotifyPropertyChanged 接口(为了简化实现,建议继承 Common/BindableBase.cs 这个类)
* PropertyChanged - 对象的属性的值发生变化时触发的事件
*/ using System;
using System.ComponentModel;
using Windows.System.Threading;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Bind
{
public sealed partial class 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 接口)
_employee = new Employee();
_employee.Name = "webabcd";
_employee.Age = 33;
_employee.IsMale = true; // Employee 对象的属性的值发生变化时触发的事件(源自 INotifyPropertyChanged 接口)
_employee.PropertyChanged += _employee_PropertyChanged; // 指定数据上下文(绑定的数据源)
root.DataContext = _employee; // 每 5 秒更新一次数据
ThreadPoolTimer.CreatePeriodicTimer
(
(timer) =>
{
var ignored = Dispatcher.RunAsync
(
CoreDispatcherPriority.Normal,
() =>
{
Random random = new Random();
_employee.Age = random.Next(10, 100);
_employee.IsMale = random.Next() % 2 == 0 ? true : false;
}
);
},
TimeSpan.FromMilliseconds(5000)
);
} // 每次属性的值发生变化时,显示变化后的结果
void _employee_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
lblMsg.Text = "属性:“" + e.PropertyName + "”的值发生了变化";
lblMsg.Text += Environment.NewLine;
lblMsg.Text += string.Format("当前的值为:Name-{0}, Age-{1}, IsMale-{2}", _employee.Name, _employee.Age, _employee.IsMale);
}
}
}

2、演示如何通过 x:Bind 绑定对象
Bind/BindModel.xaml

<Page
x:Class="Windows10.Bind.BindModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <TextBox Text="{x:Bind CurrentEmployee.Name, Mode=TwoWay}" Margin="5" />
<TextBox Text="{x:Bind CurrentEmployee.Age, Mode=TwoWay}" Margin="5" />
<ToggleSwitch IsOn="{x:Bind CurrentEmployee.IsMale, Mode=TwoWay}" OffContent="女" OnContent="男" Header="性别" Margin="5" /> </StackPanel>
</Grid>
</Page>

Bind/BindModel.xaml.cs

/*
* 演示 x:Bind 绑定的相关知识点
*
*
* 如果需要数据源在属性值发生变化时对外通知,则需要实现 INotifyPropertyChanged 接口(为了简化实现,建议继承 Common/BindableBase.cs 这个类)
* PropertyChanged - 对象的属性的值发生变化时触发的事件
*/ using System;
using System.Collections.ObjectModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Bind
{
// x:Bind 的数据上下文就是它所属的 Page 或 UserControl
public sealed partial class BindDemo : Page
{
public BindDemo()
{
this.InitializeComponent();
} // 事件绑定到方法,无参数
private void EventBindNoArgs()
{
CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
} // 事件绑定到方法,参数与对应的事件的参数相同
private void EventBindRegularArgs(object sender, RoutedEventArgs e)
{
CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
} // 事件绑定到方法,参数与对应的事件的参数相同,但是其中的事件参数为 object 类型
private void EventBindBaseArgs(object sender, object e)
{
CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
} public Employee CurrentEmployee { get; set; } = new Employee() { Name = "wanglei", Age = 36, IsMale = true }; public ObservableCollection<Employee> AllEmployees { get; set; } = TestData.GetEmployees(5);
}
}

3、示如何通过 Binding 绑定集合
Bind/BindingCollection.xaml

<Page
x:Class="Windows10.Bind.BindingCollection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="10 0 10 10"> <Button Name="btnDelete" Content="删除第 1 条数据" Click="btnDelete_Click" Margin="5" />
<Button Name="btnUpdate" Content="更新前 2 条数据" Click="btnUpdate_Click" Margin="5" /> <ListView x:Name="listView" Margin="5">
<ListView.ItemTemplate>
<DataTemplate>
<Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left">
<TextBlock Text="{Binding Name}" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> </StackPanel>
</Grid>
</Page>

Bind/BindingCollection.xaml.cs

/*
* 演示如何通过 Binding 绑定集合
*
*
* 如果需要集合数据源在数据添加,删除,更新时对外通知,则需要实现 INotifyCollectionChanged 接口
* CollectionChanged - 集合数据在发生添加,删除,更新时触发的事件
*/ using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Bind
{
public sealed partial class BindingCollection : Page
{
// ObservableCollection<T> 实现了 INotifyCollectionChanged 接口
private ObservableCollection<Employee> _employees; public BindingCollection()
{
this.InitializeComponent(); this.Loaded += BindingCollection_Loaded;
} void BindingCollection_Loaded(object sender, RoutedEventArgs e)
{
_employees = new ObservableCollection<Employee>(TestData.GetEmployees()); // 集合数据在发生添加,删除,更新时触发的事件(源自 INotifyCollectionChanged 接口)
_employees.CollectionChanged += _employees_CollectionChanged; // 指定 ListView 的数据源
listView.ItemsSource = _employees;
} void _employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
/*
* e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举)
* Add, Remove, Replace, Move, Reset
* e.OldItems - Remove, Replace, Move 操作时影响的数据列表
* e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引
* e.NewItems - 更改中所涉及的新的数据列表
* e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引
*/
} private void btnDelete_Click(object sender, RoutedEventArgs e)
{
// 此处的通知来自 INotifyCollectionChanged 接口
_employees.RemoveAt(0);
} private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
Random random = new Random(); // 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee
_employees.First().Name = random.Next(1000, 10000).ToString(); // 此处的通知来自 INotifyCollectionChanged 接口
_employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };
}
}
}

4、演示如何通过 x:Bind 绑定集合
Bind/BindCollection.xaml

<Page
x:Class="Windows10.Bind.BindCollection"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Bind"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:common="using:Windows10.Common"
mc:Ignorable="d"> <Grid Background="Transparent">
<StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="10 0 10 10"> <Button Name="btnDelete" Content="删除第 1 条数据" Click="btnDelete_Click" Margin="5" />
<Button Name="btnUpdate" Content="更新前 2 条数据" Click="btnUpdate_Click" Margin="5" /> <ListView x:Name="listView" ItemsSource="{x:Bind Employees}" Margin="5">
<ListView.ItemTemplate>
<DataTemplate x:DataType="common:Employee">
<Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left">
<TextBlock Text="{x:Bind Name, Mode=OneWay}" />
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView> </StackPanel>
</Grid>
</Page>

Bind/BindCollection.xaml.cs

/*
* 演示如何通过 x:Bind 绑定集合
*
*
* 如果需要集合数据源在数据添加,删除,更新时对外通知,则需要实现 INotifyCollectionChanged 接口
* CollectionChanged - 集合数据在发生添加,删除,更新时触发的事件
*/ using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows10.Common; namespace Windows10.Bind
{
public sealed partial class BindCollection : Page
{
// 数据源
// ObservableCollection<T> 实现了 INotifyCollectionChanged 接口
public ObservableCollection<Employee> Employees { get; set; } = new ObservableCollection<Employee>(TestData.GetEmployees()); public BindCollection()
{
this.InitializeComponent(); this.Loaded += BindCollection_Loaded;
} void BindCollection_Loaded(object sender, RoutedEventArgs e)
{
// 集合数据在发生添加,删除,更新时触发的事件(源自 INotifyCollectionChanged 接口)
Employees.CollectionChanged += Employees_CollectionChanged;
} void Employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
/*
* e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举)
* Add, Remove, Replace, Move, Reset
* e.OldItems - Remove, Replace, Move 操作时影响的数据列表
* e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引
* e.NewItems - 更改中所涉及的新的数据列表
* e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引
*/
} private void btnDelete_Click(object sender, RoutedEventArgs e)
{
// 此处的通知来自 INotifyCollectionChanged 接口
Employees.RemoveAt(0);
} private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
Random random = new Random(); // 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee
Employees.First().Name = random.Next(1000, 10000).ToString(); // 此处的通知来自 INotifyCollectionChanged 接口
Employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };
}
}
}

绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合的更多相关文章

  1. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  2. springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定

    springmvc学习笔记(13)-springmvc注解开发之集合类型參数绑定 标签: springmvc springmvc学习笔记13-springmvc注解开发之集合类型參数绑定 数组绑定 需 ...

  3. SQL state [72000]; error code [1461]; ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值 ; nested exception is java.sql.BatchUpdateException: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值

    本文转自  https://www.cnblogs.com/yingsong/p/5685790.html 原 因:某一个字段本为varchar2(1024),但是实际要插入的值超过varchar2允 ...

  4. JavaScript对象(一)——Function对象

    写在最前面: 对象只是带有属性和方法的特殊数据类型(js的7种数据类型:字符串,数字,布尔,数组,对象,null,undefined). JavaScript是面向对象的语言,但是JavaScript ...

  5. 程序猿都没对象,JS竟然有对象?

    现在做项目基本是套用框架,不论是网上的前端还是后端框架,也会寻找一些封装好的插件拿来即用,但还是希望拿来时最好自己过后再回过头了解里面的原理,学习里面优秀的东西,不论代码封装性,还是小到命名. 好吧, ...

  6. 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。

    在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...

  7. JSP 4个域对象-9个内置对象-11个EL隐式对象

    一. 四大域对象 1. PageContext :页面范围的数据 2. ServletRequest:请求范围的数据 3. HttpSession:会话范围的数据 4. ServletContext: ...

  8. FastReport报表对象介绍一:“Text”对象

    FastReport中文网 http://www.fastreportcn.com/Article/70.html ------------------------------------------ ...

  9. jq对象转为dom对象:$(".div1")[0] dom对象转为jq对象:$(dom对象)

    <!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...

随机推荐

  1. HTML 学习笔记 CSS3 (边框)

    CSS3边框 通过CSS3边框 你能够创建远角边框 向矩形边框添加阴影 使用图片来绘制边框 . CSS3的边框属性 主要包含以下几种 border-radius 边框圆角 box-shadow 边框阴 ...

  2. githup上传代码

    把自己本地东西上传到GitHup上. 本文内容来自于http://blog.csdn.net/yuanzichao/article/details/44922593 1.安装msysgit和Torto ...

  3. css3 box-sizing属性

    个人总结: 如果需要兼容IE6 IE7,使用content-box.现在流行bootstrap3,需注意它使用的是border-box. box-sizing属性可以为三个值之一:content-bo ...

  4. JavaScript中的this关键字

    在JavaScript中,函数的this关键字的行为与其他语言相比有很多不同.在JavaScript的严格模式和非严格模式下也略有区别. 在绝大多数情况下,函数的调用方式决定了this的值.this不 ...

  5. Handler 消息传递机制

    1,Handler 的概念Handler 是用来干什么的?1)执行计划任务,可以在预定的时间执行某些任务,可以模拟定时器 2)线程间通信.在Android的应用启动时,会创建一个主线程,主线程会创建一 ...

  6. JDK动态代理和CGLib动态代理简单演示

    JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期间创建接口的代理实例. 一.首先我们进行JDK动态代理的演示. 现在我们有一个简单的业务接口Saying,如下: package te ...

  7. 塔吊力矩限制器,塔吊黑匣子,塔吊电脑,tower crane

    塔机力矩限制器,tower crane 适用于各种类型的固定臂塔机和可变臂塔机 塔机力矩限制器是塔式起重机机械的安全保护装置,本产品采用32位高性能微处理器为硬件平台,软件算法采用国内最先进的三滑轮取 ...

  8. FineUI小技巧(3)表格导出与文件下载

    需求描述 实际应用中,我们可能需要导出表格内容,或者在页面回发时根据用户权限下载文件(注意,这里的导出与下载,都是在后台进行的,和普通的一个链接下载文件不同). 点击按钮导出表格 由于FineUI 默 ...

  9. 理解JavaScript中的参数传递 - leetcode189. Rotate Array

    1.关于leetcode 这是第一篇关于leetcode的题解,就先扯点关于leetcode的话. 其实很早前就在博客园看到过leetcode一些题解,总以为跟一般OJ大同小异,直到最近点开了一篇博文 ...

  10. operating expense & captial expenditure

    营运成本(营业成本, operating expense, OPEX) 指的是运行企业的持续性.消耗性的支出,与之对照的是资本支出(captial expenditure, CAPEX).例如:购买影 ...