背水一战 Windows 10 之 绑定

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

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

  1. <Page
  2. x:Class="Windows10.Bind.BindingModel"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Name="root" Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock Name="lblMsg" Margin="5" />
  14.  
  15. <TextBox Text="{Binding Name, Mode=TwoWay}" Margin="5" />
  16. <TextBox Text="{Binding Age, Mode=TwoWay}" Margin="5" />
  17. <ToggleSwitch IsOn="{Binding IsMale, Mode=TwoWay}" OffContent="女" OnContent="" Header="性别" Margin="5" />
  18.  
  19. </StackPanel>
  20. </Grid>
  21. </Page>

Bind/BindingModel.xaml.cs

  1. /*
  2. * 演示如何通过 Binding 绑定对象
  3. *
  4. *
  5. * 如果需要数据源在属性值发生变化时对外通知,则需要实现 INotifyPropertyChanged 接口(为了简化实现,建议继承 Common/BindableBase.cs 这个类)
  6. * PropertyChanged - 对象的属性的值发生变化时触发的事件
  7. */
  8.  
  9. using System;
  10. using System.ComponentModel;
  11. using Windows.System.Threading;
  12. using Windows.UI.Core;
  13. using Windows.UI.Xaml.Controls;
  14. using Windows10.Common;
  15.  
  16. namespace Windows10.Bind
  17. {
  18. public sealed partial class BindingModel : Page
  19. {
  20. private Employee _employee;
  21.  
  22. public BindingModel()
  23. {
  24. this.InitializeComponent();
  25.  
  26. this.Loaded += BindingModel_Loaded;
  27. }
  28.  
  29. void BindingModel_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
  30. {
  31. // 创建一个需要绑定的实体对象(注:Employee 实现了 INotifyPropertyChanged 接口)
  32. _employee = new Employee();
  33. _employee.Name = "webabcd";
  34. _employee.Age = 33;
  35. _employee.IsMale = true;
  36.  
  37. // Employee 对象的属性的值发生变化时触发的事件(源自 INotifyPropertyChanged 接口)
  38. _employee.PropertyChanged += _employee_PropertyChanged;
  39.  
  40. // 指定数据上下文(绑定的数据源)
  41. root.DataContext = _employee;
  42.  
  43. // 每 5 秒更新一次数据
  44. ThreadPoolTimer.CreatePeriodicTimer
  45. (
  46. (timer) =>
  47. {
  48. var ignored = Dispatcher.RunAsync
  49. (
  50. CoreDispatcherPriority.Normal,
  51. () =>
  52. {
  53. Random random = new Random();
  54. _employee.Age = random.Next(10, 100);
  55. _employee.IsMale = random.Next() % 2 == 0 ? true : false;
  56. }
  57. );
  58. },
  59. TimeSpan.FromMilliseconds(5000)
  60. );
  61. }
  62.  
  63. // 每次属性的值发生变化时,显示变化后的结果
  64. void _employee_PropertyChanged(object sender, PropertyChangedEventArgs e)
  65. {
  66. lblMsg.Text = "属性:“" + e.PropertyName + "”的值发生了变化";
  67. lblMsg.Text += Environment.NewLine;
  68. lblMsg.Text += string.Format("当前的值为:Name-{0}, Age-{1}, IsMale-{2}", _employee.Name, _employee.Age, _employee.IsMale);
  69. }
  70. }
  71. }

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

  1. <Page
  2. x:Class="Windows10.Bind.BindModel"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Margin="10 0 10 10">
  12.  
  13. <TextBlock Name="lblMsg" Margin="5" />
  14.  
  15. <TextBox Text="{x:Bind CurrentEmployee.Name, Mode=TwoWay}" Margin="5" />
  16. <TextBox Text="{x:Bind CurrentEmployee.Age, Mode=TwoWay}" Margin="5" />
  17. <ToggleSwitch IsOn="{x:Bind CurrentEmployee.IsMale, Mode=TwoWay}" OffContent="女" OnContent="" Header="性别" Margin="5" />
  18.  
  19. </StackPanel>
  20. </Grid>
  21. </Page>

Bind/BindModel.xaml.cs

  1. /*
  2. * 演示 x:Bind 绑定的相关知识点
  3. *
  4. *
  5. * 如果需要数据源在属性值发生变化时对外通知,则需要实现 INotifyPropertyChanged 接口(为了简化实现,建议继承 Common/BindableBase.cs 这个类)
  6. * PropertyChanged - 对象的属性的值发生变化时触发的事件
  7. */
  8.  
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using Windows.UI.Xaml;
  12. using Windows.UI.Xaml.Controls;
  13. using Windows10.Common;
  14.  
  15. namespace Windows10.Bind
  16. {
  17. // x:Bind 的数据上下文就是它所属的 Page 或 UserControl
  18. public sealed partial class BindDemo : Page
  19. {
  20. public BindDemo()
  21. {
  22. this.InitializeComponent();
  23. }
  24.  
  25. // 事件绑定到方法,无参数
  26. private void EventBindNoArgs()
  27. {
  28. CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
  29. }
  30.  
  31. // 事件绑定到方法,参数与对应的事件的参数相同
  32. private void EventBindRegularArgs(object sender, RoutedEventArgs e)
  33. {
  34. CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
  35. }
  36.  
  37. // 事件绑定到方法,参数与对应的事件的参数相同,但是其中的事件参数为 object 类型
  38. private void EventBindBaseArgs(object sender, object e)
  39. {
  40. CurrentEmployee.Name = "wanglei" + new Random().Next(1000, 10000).ToString();
  41. }
  42.  
  43. public Employee CurrentEmployee { get; set; } = new Employee() { Name = "wanglei", Age = 36, IsMale = true };
  44.  
  45. public ObservableCollection<Employee> AllEmployees { get; set; } = TestData.GetEmployees(5);
  46. }
  47. }

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

  1. <Page
  2. x:Class="Windows10.Bind.BindingCollection"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11. <StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="10 0 10 10">
  12.  
  13. <Button Name="btnDelete" Content="删除第 1 条数据" Click="btnDelete_Click" Margin="5" />
  14. <Button Name="btnUpdate" Content="更新前 2 条数据" Click="btnUpdate_Click" Margin="5" />
  15.  
  16. <ListView x:Name="listView" Margin="5">
  17. <ListView.ItemTemplate>
  18. <DataTemplate>
  19. <Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left">
  20. <TextBlock Text="{Binding Name}" />
  21. </Border>
  22. </DataTemplate>
  23. </ListView.ItemTemplate>
  24. </ListView>
  25.  
  26. </StackPanel>
  27. </Grid>
  28. </Page>

Bind/BindingCollection.xaml.cs

  1. /*
  2. * 演示如何通过 Binding 绑定集合
  3. *
  4. *
  5. * 如果需要集合数据源在数据添加,删除,更新时对外通知,则需要实现 INotifyCollectionChanged 接口
  6. * CollectionChanged - 集合数据在发生添加,删除,更新时触发的事件
  7. */
  8.  
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using System.Collections.Specialized;
  12. using System.Linq;
  13. using Windows.UI.Xaml;
  14. using Windows.UI.Xaml.Controls;
  15. using Windows10.Common;
  16.  
  17. namespace Windows10.Bind
  18. {
  19. public sealed partial class BindingCollection : Page
  20. {
  21. // ObservableCollection<T> 实现了 INotifyCollectionChanged 接口
  22. private ObservableCollection<Employee> _employees;
  23.  
  24. public BindingCollection()
  25. {
  26. this.InitializeComponent();
  27.  
  28. this.Loaded += BindingCollection_Loaded;
  29. }
  30.  
  31. void BindingCollection_Loaded(object sender, RoutedEventArgs e)
  32. {
  33. _employees = new ObservableCollection<Employee>(TestData.GetEmployees());
  34.  
  35. // 集合数据在发生添加,删除,更新时触发的事件(源自 INotifyCollectionChanged 接口)
  36. _employees.CollectionChanged += _employees_CollectionChanged;
  37.  
  38. // 指定 ListView 的数据源
  39. listView.ItemsSource = _employees;
  40. }
  41.  
  42. void _employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  43. {
  44. /*
  45. * e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举)
  46. * Add, Remove, Replace, Move, Reset
  47. * e.OldItems - Remove, Replace, Move 操作时影响的数据列表
  48. * e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引
  49. * e.NewItems - 更改中所涉及的新的数据列表
  50. * e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引
  51. */
  52. }
  53.  
  54. private void btnDelete_Click(object sender, RoutedEventArgs e)
  55. {
  56. // 此处的通知来自 INotifyCollectionChanged 接口
  57. _employees.RemoveAt(0);
  58. }
  59.  
  60. private void btnUpdate_Click(object sender, RoutedEventArgs e)
  61. {
  62. Random random = new Random();
  63.  
  64. // 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee
  65. _employees.First().Name = random.Next(1000, 10000).ToString();
  66.  
  67. // 此处的通知来自 INotifyCollectionChanged 接口
  68. _employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };
  69. }
  70. }
  71. }

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

  1. <Page
  2. x:Class="Windows10.Bind.BindCollection"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows10.Bind"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8.  
  9. xmlns:common="using:Windows10.Common"
  10. mc:Ignorable="d">
  11.  
  12. <Grid Background="Transparent">
  13. <StackPanel Orientation="Vertical" VerticalAlignment="Top" Margin="10 0 10 10">
  14.  
  15. <Button Name="btnDelete" Content="删除第 1 条数据" Click="btnDelete_Click" Margin="5" />
  16. <Button Name="btnUpdate" Content="更新前 2 条数据" Click="btnUpdate_Click" Margin="5" />
  17.  
  18. <ListView x:Name="listView" ItemsSource="{x:Bind Employees}" Margin="5">
  19. <ListView.ItemTemplate>
  20. <DataTemplate x:DataType="common:Employee">
  21. <Border Background="Blue" Width="200" CornerRadius="3" HorizontalAlignment="Left">
  22. <TextBlock Text="{x:Bind Name, Mode=OneWay}" />
  23. </Border>
  24. </DataTemplate>
  25. </ListView.ItemTemplate>
  26. </ListView>
  27.  
  28. </StackPanel>
  29. </Grid>
  30. </Page>

Bind/BindCollection.xaml.cs

  1. /*
  2. * 演示如何通过 x:Bind 绑定集合
  3. *
  4. *
  5. * 如果需要集合数据源在数据添加,删除,更新时对外通知,则需要实现 INotifyCollectionChanged 接口
  6. * CollectionChanged - 集合数据在发生添加,删除,更新时触发的事件
  7. */
  8.  
  9. using System;
  10. using System.Collections.ObjectModel;
  11. using System.Collections.Specialized;
  12. using System.Linq;
  13. using Windows.UI.Xaml;
  14. using Windows.UI.Xaml.Controls;
  15. using Windows10.Common;
  16.  
  17. namespace Windows10.Bind
  18. {
  19. public sealed partial class BindCollection : Page
  20. {
  21. // 数据源
  22. // ObservableCollection<T> 实现了 INotifyCollectionChanged 接口
  23. public ObservableCollection<Employee> Employees { get; set; } = new ObservableCollection<Employee>(TestData.GetEmployees());
  24.  
  25. public BindCollection()
  26. {
  27. this.InitializeComponent();
  28.  
  29. this.Loaded += BindCollection_Loaded;
  30. }
  31.  
  32. void BindCollection_Loaded(object sender, RoutedEventArgs e)
  33. {
  34. // 集合数据在发生添加,删除,更新时触发的事件(源自 INotifyCollectionChanged 接口)
  35. Employees.CollectionChanged += Employees_CollectionChanged;
  36. }
  37.  
  38. void Employees_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  39. {
  40. /*
  41. * e.Action - 引发此事件的操作类型(NotifyCollectionChangedAction 枚举)
  42. * Add, Remove, Replace, Move, Reset
  43. * e.OldItems - Remove, Replace, Move 操作时影响的数据列表
  44. * e.OldStartingIndex - Remove, Replace, Move 操作发生处的索引
  45. * e.NewItems - 更改中所涉及的新的数据列表
  46. * e.NewStartingIndex - 更改中所涉及的新的数据列表的发生处的索引
  47. */
  48. }
  49.  
  50. private void btnDelete_Click(object sender, RoutedEventArgs e)
  51. {
  52. // 此处的通知来自 INotifyCollectionChanged 接口
  53. Employees.RemoveAt(0);
  54. }
  55.  
  56. private void btnUpdate_Click(object sender, RoutedEventArgs e)
  57. {
  58. Random random = new Random();
  59.  
  60. // 此处的通知来自实现了 INotifyPropertyChanged 接口的 Employee
  61. Employees.First().Name = random.Next(1000, 10000).ToString();
  62.  
  63. // 此处的通知来自 INotifyCollectionChanged 接口
  64. Employees[1] = new Employee() { Name = random.Next(1000, 10000).ToString() };
  65. }
  66. }
  67. }

绑定: 通过 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. 在VMware Workstation上安装CentOS6.5系统步

    在VMware Workstation上安装CentOS6.5系统步骤 听语音 | 浏览:147 | 更新:2016-07-28 15:45 | 标签:安装 虚拟机 CENTOS 1 2 3 4 5 ...

  2. NOIP2015 子串

    #149. [NOIP2015]子串 有两个仅包含小写英文字母的字符串 AA 和 BB. 现在要从字符串 AA 中取出 kk 个互不重叠的非空子串,然后把这 kk 个子串按照其在字符串 AA 中出现的 ...

  3. Android保存ArrayList至SharedPreferences

    保存ArrayList至SharedPreferences 其中ArrayList中每个元素为String List<String> environmentList = new Array ...

  4. oracle中substr() instr() 用法

    --substr(字符串,截取开始位置,截取长度)=返回截取的字 ,) from dual;--返回结果为:m ,) from dual;--返回结果为:m--说明0和1都表示截取的位置为第一个字符 ...

  5. Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package 1.0.1 supports: net (.NETFramework,Version=v0.0)

    Nuget自己打包引用的时候出现错误:Package is not compatible with netcoreapp1.0 (.NETCoreApp,Version=v1.0). Package ...

  6. 【转】【MySql】mysql存储过程中的异常处理

    定义异常捕获类型及处理方法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement ...

  7. PhoneGap: Android平台入门例子(Hello World)

    Hello World Demo: http://docs.phonegap.com/en/2.0.0/guide_getting-started_android_index.md.html#Gett ...

  8. Theano2.1.14-基础知识之理解为了速度和正确性的内存别名

    来自:http://deeplearning.net/software/theano/tutorial/aliasing.html Understanding Memory Aliasing for ...

  9. Windows 部署 Redis 群集

    1,下载Redis for windows 的最新版本,解压到 c:\Redis 目录下备用https://github.com/MSOpenTech/redis/releases当前我使用的是 3. ...

  10. java并发:简单面试问题集锦

    多线程:Simultaneous Multithreading,简称SMT. 并行.并发 并行性(parallelism)指两个或两个以上的事件在同一时刻发生,在多道程序环境下,并行性使多个程序同一时 ...